it8152.c 9.1 KB

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