pci-ar724x.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.h>
  12. #define reg_read(_phys) (*(unsigned int *) KSEG1ADDR(_phys))
  13. #define reg_write(_phys, _val) ((*(unsigned int *) KSEG1ADDR(_phys)) = (_val))
  14. #define AR724X_PCI_DEV_BASE 0x14000000
  15. #define AR724X_PCI_MEM_BASE 0x10000000
  16. #define AR724X_PCI_MEM_SIZE 0x08000000
  17. static DEFINE_SPINLOCK(ar724x_pci_lock);
  18. static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  19. int size, uint32_t *value)
  20. {
  21. unsigned long flags, addr, tval, mask;
  22. if (devfn)
  23. return PCIBIOS_DEVICE_NOT_FOUND;
  24. if (where & (size - 1))
  25. return PCIBIOS_BAD_REGISTER_NUMBER;
  26. spin_lock_irqsave(&ar724x_pci_lock, flags);
  27. switch (size) {
  28. case 1:
  29. addr = where & ~3;
  30. mask = 0xff000000 >> ((where % 4) * 8);
  31. tval = reg_read(AR724X_PCI_DEV_BASE + addr);
  32. tval = tval & ~mask;
  33. *value = (tval >> ((4 - (where % 4))*8));
  34. break;
  35. case 2:
  36. addr = where & ~3;
  37. mask = 0xffff0000 >> ((where % 4)*8);
  38. tval = reg_read(AR724X_PCI_DEV_BASE + addr);
  39. tval = tval & ~mask;
  40. *value = (tval >> ((4 - (where % 4))*8));
  41. break;
  42. case 4:
  43. *value = reg_read(AR724X_PCI_DEV_BASE + where);
  44. break;
  45. default:
  46. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  47. return PCIBIOS_BAD_REGISTER_NUMBER;
  48. }
  49. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  50. return PCIBIOS_SUCCESSFUL;
  51. }
  52. static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  53. int size, uint32_t value)
  54. {
  55. unsigned long flags, tval, addr, mask;
  56. if (devfn)
  57. return PCIBIOS_DEVICE_NOT_FOUND;
  58. if (where & (size - 1))
  59. return PCIBIOS_BAD_REGISTER_NUMBER;
  60. spin_lock_irqsave(&ar724x_pci_lock, flags);
  61. switch (size) {
  62. case 1:
  63. addr = (AR724X_PCI_DEV_BASE + where) & ~3;
  64. mask = 0xff000000 >> ((where % 4)*8);
  65. tval = reg_read(addr);
  66. tval = tval & ~mask;
  67. tval |= (value << ((4 - (where % 4))*8)) & mask;
  68. reg_write(addr, tval);
  69. break;
  70. case 2:
  71. addr = (AR724X_PCI_DEV_BASE + where) & ~3;
  72. mask = 0xffff0000 >> ((where % 4)*8);
  73. tval = reg_read(addr);
  74. tval = tval & ~mask;
  75. tval |= (value << ((4 - (where % 4))*8)) & mask;
  76. reg_write(addr, tval);
  77. break;
  78. case 4:
  79. reg_write((AR724X_PCI_DEV_BASE + where), value);
  80. break;
  81. default:
  82. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  83. return PCIBIOS_BAD_REGISTER_NUMBER;
  84. }
  85. spin_unlock_irqrestore(&ar724x_pci_lock, flags);
  86. return PCIBIOS_SUCCESSFUL;
  87. }
  88. static struct pci_ops ar724x_pci_ops = {
  89. .read = ar724x_pci_read,
  90. .write = ar724x_pci_write,
  91. };
  92. static struct resource ar724x_io_resource = {
  93. .name = "PCI IO space",
  94. .start = 0,
  95. .end = 0,
  96. .flags = IORESOURCE_IO,
  97. };
  98. static struct resource ar724x_mem_resource = {
  99. .name = "PCI memory space",
  100. .start = AR724X_PCI_MEM_BASE,
  101. .end = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1,
  102. .flags = IORESOURCE_MEM,
  103. };
  104. static struct pci_controller ar724x_pci_controller = {
  105. .pci_ops = &ar724x_pci_ops,
  106. .io_resource = &ar724x_io_resource,
  107. .mem_resource = &ar724x_mem_resource,
  108. };
  109. int __init ar724x_pcibios_init(void)
  110. {
  111. register_pci_controller(&ar724x_pci_controller);
  112. return PCIBIOS_SUCCESSFUL;
  113. }