it8152.c 9.6 KB

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