it8152.c 9.2 KB

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