it8152.c 9.6 KB

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