it8152.c 9.6 KB

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