it8152.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * linux/arch/arm/common/it8152.c
  3. *
  4. * Copyright Compulab Ltd, 2002-2007
  5. * Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * The DMA bouncing part is taken from arch/arm/mach-ixp4xx/common-pci.c
  8. * (see this file for respective copyrights)
  9. *
  10. * Thanks to Guennadi Liakhovetski <gl@dsa-ac.de> for IRQ enumberation
  11. * and demux code.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mm.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/irq.h>
  26. #include <linux/io.h>
  27. #include <asm/mach/pci.h>
  28. #include <asm/hardware/it8152.h>
  29. #define MAX_SLOTS 21
  30. static void it8152_mask_irq(unsigned int irq)
  31. {
  32. if (irq >= IT8152_LD_IRQ(0)) {
  33. __raw_writel((__raw_readl(IT8152_INTC_LDCNIMR) |
  34. (1 << (irq - IT8152_LD_IRQ(0)))),
  35. IT8152_INTC_LDCNIMR);
  36. } else if (irq >= IT8152_LP_IRQ(0)) {
  37. __raw_writel((__raw_readl(IT8152_INTC_LPCNIMR) |
  38. (1 << (irq - IT8152_LP_IRQ(0)))),
  39. IT8152_INTC_LPCNIMR);
  40. } else if (irq >= IT8152_PD_IRQ(0)) {
  41. __raw_writel((__raw_readl(IT8152_INTC_PDCNIMR) |
  42. (1 << (irq - IT8152_PD_IRQ(0)))),
  43. IT8152_INTC_PDCNIMR);
  44. }
  45. }
  46. static void it8152_unmask_irq(unsigned int irq)
  47. {
  48. if (irq >= IT8152_LD_IRQ(0)) {
  49. __raw_writel((__raw_readl(IT8152_INTC_LDCNIMR) &
  50. ~(1 << (irq - IT8152_LD_IRQ(0)))),
  51. IT8152_INTC_LDCNIMR);
  52. } else if (irq >= IT8152_LP_IRQ(0)) {
  53. __raw_writel((__raw_readl(IT8152_INTC_LPCNIMR) &
  54. ~(1 << (irq - IT8152_LP_IRQ(0)))),
  55. IT8152_INTC_LPCNIMR);
  56. } else if (irq >= IT8152_PD_IRQ(0)) {
  57. __raw_writel((__raw_readl(IT8152_INTC_PDCNIMR) &
  58. ~(1 << (irq - IT8152_PD_IRQ(0)))),
  59. IT8152_INTC_PDCNIMR);
  60. }
  61. }
  62. static struct irq_chip it8152_irq_chip = {
  63. .name = "it8152",
  64. .ack = it8152_mask_irq,
  65. .mask = it8152_mask_irq,
  66. .unmask = it8152_unmask_irq,
  67. };
  68. void it8152_init_irq(void)
  69. {
  70. int irq;
  71. __raw_writel((0xffff), IT8152_INTC_PDCNIMR);
  72. __raw_writel((0), IT8152_INTC_PDCNIRR);
  73. __raw_writel((0xffff), IT8152_INTC_LPCNIMR);
  74. __raw_writel((0), IT8152_INTC_LPCNIRR);
  75. __raw_writel((0xffff), IT8152_INTC_LDCNIMR);
  76. __raw_writel((0), IT8152_INTC_LDCNIRR);
  77. for (irq = IT8152_IRQ(0); irq <= IT8152_LAST_IRQ; irq++) {
  78. set_irq_chip(irq, &it8152_irq_chip);
  79. set_irq_handler(irq, handle_level_irq);
  80. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  81. }
  82. }
  83. void it8152_irq_demux(unsigned int irq, struct irq_desc *desc)
  84. {
  85. int bits_pd, bits_lp, bits_ld;
  86. int i;
  87. while (1) {
  88. /* Read all */
  89. bits_pd = __raw_readl(IT8152_INTC_PDCNIRR);
  90. bits_lp = __raw_readl(IT8152_INTC_LPCNIRR);
  91. bits_ld = __raw_readl(IT8152_INTC_LDCNIRR);
  92. /* Ack */
  93. __raw_writel((~bits_pd), IT8152_INTC_PDCNIRR);
  94. __raw_writel((~bits_lp), IT8152_INTC_LPCNIRR);
  95. __raw_writel((~bits_ld), IT8152_INTC_LDCNIRR);
  96. if (!(bits_ld | bits_lp | bits_pd)) {
  97. /* Re-read to guarantee, that there was a moment of
  98. time, when they all three were 0. */
  99. bits_pd = __raw_readl(IT8152_INTC_PDCNIRR);
  100. bits_lp = __raw_readl(IT8152_INTC_LPCNIRR);
  101. bits_ld = __raw_readl(IT8152_INTC_LDCNIRR);
  102. if (!(bits_ld | bits_lp | bits_pd))
  103. return;
  104. }
  105. bits_pd &= ((1 << IT8152_PD_IRQ_COUNT) - 1);
  106. while (bits_pd) {
  107. i = __ffs(bits_pd);
  108. generic_handle_irq(IT8152_PD_IRQ(i));
  109. bits_pd &= ~(1 << i);
  110. }
  111. bits_lp &= ((1 << IT8152_LP_IRQ_COUNT) - 1);
  112. while (bits_lp) {
  113. i = __ffs(bits_lp);
  114. generic_handle_irq(IT8152_LP_IRQ(i));
  115. bits_lp &= ~(1 << i);
  116. }
  117. bits_ld &= ((1 << IT8152_LD_IRQ_COUNT) - 1);
  118. while (bits_ld) {
  119. i = __ffs(bits_ld);
  120. generic_handle_irq(IT8152_LD_IRQ(i));
  121. bits_ld &= ~(1 << i);
  122. }
  123. }
  124. }
  125. /* mapping for on-chip devices */
  126. int __init it8152_pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  127. {
  128. if ((dev->vendor == PCI_VENDOR_ID_ITE) &&
  129. (dev->device == PCI_DEVICE_ID_ITE_8152)) {
  130. if ((dev->class >> 8) == PCI_CLASS_MULTIMEDIA_AUDIO)
  131. return IT8152_AUDIO_INT;
  132. if ((dev->class >> 8) == PCI_CLASS_SERIAL_USB)
  133. return IT8152_USB_INT;
  134. if ((dev->class >> 8) == PCI_CLASS_SYSTEM_DMA)
  135. return IT8152_CDMA_INT;
  136. }
  137. return 0;
  138. }
  139. static unsigned long it8152_pci_dev_base_address(struct pci_bus *bus,
  140. unsigned int devfn)
  141. {
  142. unsigned long addr = 0;
  143. if (bus->number == 0) {
  144. if (devfn < PCI_DEVFN(MAX_SLOTS, 0))
  145. addr = (devfn << 8);
  146. } else
  147. addr = (bus->number << 16) | (devfn << 8);
  148. return addr;
  149. }
  150. static int it8152_pci_read_config(struct pci_bus *bus,
  151. unsigned int devfn, int where,
  152. int size, u32 *value)
  153. {
  154. unsigned long addr = it8152_pci_dev_base_address(bus, devfn);
  155. u32 v;
  156. int shift;
  157. shift = (where & 3);
  158. __raw_writel((addr + where), IT8152_PCI_CFG_ADDR);
  159. v = (__raw_readl(IT8152_PCI_CFG_DATA) >> (8 * (shift)));
  160. *value = v;
  161. return PCIBIOS_SUCCESSFUL;
  162. }
  163. static int it8152_pci_write_config(struct pci_bus *bus,
  164. unsigned int devfn, int where,
  165. int size, u32 value)
  166. {
  167. unsigned long addr = it8152_pci_dev_base_address(bus, devfn);
  168. u32 v, vtemp, mask = 0;
  169. int shift;
  170. if (size == 1)
  171. mask = 0xff;
  172. if (size == 2)
  173. mask = 0xffff;
  174. shift = (where & 3);
  175. __raw_writel((addr + where), IT8152_PCI_CFG_ADDR);
  176. vtemp = __raw_readl(IT8152_PCI_CFG_DATA);
  177. if (mask)
  178. vtemp &= ~(mask << (8 * shift));
  179. else
  180. vtemp = 0;
  181. v = (value << (8 * shift));
  182. __raw_writel((addr + where), IT8152_PCI_CFG_ADDR);
  183. __raw_writel((v | vtemp), IT8152_PCI_CFG_DATA);
  184. return PCIBIOS_SUCCESSFUL;
  185. }
  186. static struct pci_ops it8152_ops = {
  187. .read = it8152_pci_read_config,
  188. .write = it8152_pci_write_config,
  189. };
  190. static struct resource it8152_io = {
  191. .name = "IT8152 PCI I/O region",
  192. .flags = IORESOURCE_IO,
  193. };
  194. static struct resource it8152_mem = {
  195. .name = "IT8152 PCI memory region",
  196. .start = 0x10000000,
  197. .end = 0x13e00000,
  198. .flags = IORESOURCE_MEM,
  199. };
  200. /*
  201. * The following functions are needed for DMA bouncing.
  202. * ITE8152 chip can addrees up to 64MByte, so all the devices
  203. * connected to ITE8152 (PCI and USB) should have limited DMA window
  204. */
  205. /*
  206. * Setup DMA mask to 64MB on devices connected to ITE8152. Ignore all
  207. * other devices.
  208. */
  209. static int it8152_pci_platform_notify(struct device *dev)
  210. {
  211. if (dev->bus == &pci_bus_type) {
  212. if (dev->dma_mask)
  213. *dev->dma_mask = (SZ_64M - 1) | PHYS_OFFSET;
  214. dev->coherent_dma_mask = (SZ_64M - 1) | PHYS_OFFSET;
  215. dmabounce_register_dev(dev, 2048, 4096);
  216. }
  217. return 0;
  218. }
  219. static int it8152_pci_platform_notify_remove(struct device *dev)
  220. {
  221. if (dev->bus == &pci_bus_type)
  222. dmabounce_unregister_dev(dev);
  223. return 0;
  224. }
  225. int dma_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
  226. {
  227. dev_dbg(dev, "%s: dma_addr %08x, size %08x\n",
  228. __func__, dma_addr, size);
  229. return (dev->bus == &pci_bus_type) &&
  230. ((dma_addr + size - PHYS_OFFSET) >= SZ_64M);
  231. }
  232. int __init it8152_pci_setup(int nr, struct pci_sys_data *sys)
  233. {
  234. it8152_io.start = IT8152_IO_BASE + 0x12000;
  235. it8152_io.end = IT8152_IO_BASE + 0x12000 + 0x100000;
  236. sys->mem_offset = 0x10000000;
  237. sys->io_offset = IT8152_IO_BASE;
  238. if (request_resource(&ioport_resource, &it8152_io)) {
  239. printk(KERN_ERR "PCI: unable to allocate IO region\n");
  240. goto err0;
  241. }
  242. if (request_resource(&iomem_resource, &it8152_mem)) {
  243. printk(KERN_ERR "PCI: unable to allocate memory region\n");
  244. goto err1;
  245. }
  246. sys->resource[0] = &it8152_io;
  247. sys->resource[1] = &it8152_mem;
  248. if (platform_notify || platform_notify_remove) {
  249. printk(KERN_ERR "PCI: Can't use platform_notify\n");
  250. goto err2;
  251. }
  252. platform_notify = it8152_pci_platform_notify;
  253. platform_notify_remove = it8152_pci_platform_notify_remove;
  254. return 1;
  255. err2:
  256. release_resource(&it8152_io);
  257. err1:
  258. release_resource(&it8152_mem);
  259. err0:
  260. return -EBUSY;
  261. }
  262. /*
  263. * If we set up a device for bus mastering, we need to check the latency
  264. * timer as we don't have even crappy BIOSes to set it properly.
  265. * The implementation is from arch/i386/pci/i386.c
  266. */
  267. unsigned int pcibios_max_latency = 255;
  268. void pcibios_set_master(struct pci_dev *dev)
  269. {
  270. u8 lat;
  271. /* no need to update on-chip OHCI controller */
  272. if ((dev->vendor == PCI_VENDOR_ID_ITE) &&
  273. (dev->device == PCI_DEVICE_ID_ITE_8152) &&
  274. ((dev->class >> 8) == PCI_CLASS_SERIAL_USB))
  275. return;
  276. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  277. if (lat < 16)
  278. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  279. else if (lat > pcibios_max_latency)
  280. lat = pcibios_max_latency;
  281. else
  282. return;
  283. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
  284. pci_name(dev), lat);
  285. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  286. }
  287. struct pci_bus * __init it8152_pci_scan_bus(int nr, struct pci_sys_data *sys)
  288. {
  289. return pci_scan_bus(nr, &it8152_ops, sys);
  290. }