pci-ath724x.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Atheros 724x PCI support
  3. *
  4. * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/pci.h>
  11. #include <asm/mach-ath79/pci-ath724x.h>
  12. #define reg_read(_phys) (*(unsigned int *) KSEG1ADDR(_phys))
  13. #define reg_write(_phys, _val) ((*(unsigned int *) KSEG1ADDR(_phys)) = (_val))
  14. #define ATH724X_PCI_DEV_BASE 0x14000000
  15. #define ATH724X_PCI_MEM_BASE 0x10000000
  16. #define ATH724X_PCI_MEM_SIZE 0x08000000
  17. static DEFINE_SPINLOCK(ath724x_pci_lock);
  18. static struct ath724x_pci_data *pci_data;
  19. static int pci_data_size;
  20. static int ath724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  21. int size, uint32_t *value)
  22. {
  23. unsigned long flags, addr, tval, mask;
  24. if (devfn)
  25. return PCIBIOS_DEVICE_NOT_FOUND;
  26. if (where & (size - 1))
  27. return PCIBIOS_BAD_REGISTER_NUMBER;
  28. spin_lock_irqsave(&ath724x_pci_lock, flags);
  29. switch (size) {
  30. case 1:
  31. addr = where & ~3;
  32. mask = 0xff000000 >> ((where % 4) * 8);
  33. tval = reg_read(ATH724X_PCI_DEV_BASE + addr);
  34. tval = tval & ~mask;
  35. *value = (tval >> ((4 - (where % 4))*8));
  36. break;
  37. case 2:
  38. addr = where & ~3;
  39. mask = 0xffff0000 >> ((where % 4)*8);
  40. tval = reg_read(ATH724X_PCI_DEV_BASE + addr);
  41. tval = tval & ~mask;
  42. *value = (tval >> ((4 - (where % 4))*8));
  43. break;
  44. case 4:
  45. *value = reg_read(ATH724X_PCI_DEV_BASE + where);
  46. break;
  47. default:
  48. spin_unlock_irqrestore(&ath724x_pci_lock, flags);
  49. return PCIBIOS_BAD_REGISTER_NUMBER;
  50. }
  51. spin_unlock_irqrestore(&ath724x_pci_lock, flags);
  52. return PCIBIOS_SUCCESSFUL;
  53. }
  54. static int ath724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  55. int size, uint32_t value)
  56. {
  57. unsigned long flags, tval, addr, mask;
  58. if (devfn)
  59. return PCIBIOS_DEVICE_NOT_FOUND;
  60. if (where & (size - 1))
  61. return PCIBIOS_BAD_REGISTER_NUMBER;
  62. spin_lock_irqsave(&ath724x_pci_lock, flags);
  63. switch (size) {
  64. case 1:
  65. addr = (ATH724X_PCI_DEV_BASE + where) & ~3;
  66. mask = 0xff000000 >> ((where % 4)*8);
  67. tval = reg_read(addr);
  68. tval = tval & ~mask;
  69. tval |= (value << ((4 - (where % 4))*8)) & mask;
  70. reg_write(addr, tval);
  71. break;
  72. case 2:
  73. addr = (ATH724X_PCI_DEV_BASE + where) & ~3;
  74. mask = 0xffff0000 >> ((where % 4)*8);
  75. tval = reg_read(addr);
  76. tval = tval & ~mask;
  77. tval |= (value << ((4 - (where % 4))*8)) & mask;
  78. reg_write(addr, tval);
  79. break;
  80. case 4:
  81. reg_write((ATH724X_PCI_DEV_BASE + where), value);
  82. break;
  83. default:
  84. spin_unlock_irqrestore(&ath724x_pci_lock, flags);
  85. return PCIBIOS_BAD_REGISTER_NUMBER;
  86. }
  87. spin_unlock_irqrestore(&ath724x_pci_lock, flags);
  88. return PCIBIOS_SUCCESSFUL;
  89. }
  90. static struct pci_ops ath724x_pci_ops = {
  91. .read = ath724x_pci_read,
  92. .write = ath724x_pci_write,
  93. };
  94. static struct resource ath724x_io_resource = {
  95. .name = "PCI IO space",
  96. .start = 0,
  97. .end = 0,
  98. .flags = IORESOURCE_IO,
  99. };
  100. static struct resource ath724x_mem_resource = {
  101. .name = "PCI memory space",
  102. .start = ATH724X_PCI_MEM_BASE,
  103. .end = ATH724X_PCI_MEM_BASE + ATH724X_PCI_MEM_SIZE - 1,
  104. .flags = IORESOURCE_MEM,
  105. };
  106. static struct pci_controller ath724x_pci_controller = {
  107. .pci_ops = &ath724x_pci_ops,
  108. .io_resource = &ath724x_io_resource,
  109. .mem_resource = &ath724x_mem_resource,
  110. };
  111. void ath724x_pci_add_data(struct ath724x_pci_data *data, int size)
  112. {
  113. pci_data = data;
  114. pci_data_size = size;
  115. }
  116. int __init pcibios_map_irq(const struct pci_dev *dev, uint8_t slot, uint8_t pin)
  117. {
  118. unsigned int devfn = dev->devfn;
  119. int irq = -1;
  120. if (devfn > pci_data_size - 1)
  121. return irq;
  122. irq = pci_data[devfn].irq;
  123. return irq;
  124. }
  125. int pcibios_plat_dev_init(struct pci_dev *dev)
  126. {
  127. unsigned int devfn = dev->devfn;
  128. if (devfn > pci_data_size - 1)
  129. return PCIBIOS_DEVICE_NOT_FOUND;
  130. dev->dev.platform_data = pci_data[devfn].pdata;
  131. return PCIBIOS_SUCCESSFUL;
  132. }
  133. static int __init ath724x_pcibios_init(void)
  134. {
  135. register_pci_controller(&ath724x_pci_controller);
  136. return PCIBIOS_SUCCESSFUL;
  137. }
  138. arch_initcall(ath724x_pcibios_init);