Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 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 | |
| 11 | #include <linux/pci.h> |
Gabor Juhos | 659243c | 2012-03-14 10:29:23 +0100 | [diff] [blame^] | 12 | #include <asm/mach-ath79/pci.h> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 13 | |
| 14 | #define reg_read(_phys) (*(unsigned int *) KSEG1ADDR(_phys)) |
| 15 | #define reg_write(_phys, _val) ((*(unsigned int *) KSEG1ADDR(_phys)) = (_val)) |
| 16 | |
| 17 | #define ATH724X_PCI_DEV_BASE 0x14000000 |
| 18 | #define ATH724X_PCI_MEM_BASE 0x10000000 |
| 19 | #define ATH724X_PCI_MEM_SIZE 0x08000000 |
| 20 | |
| 21 | static DEFINE_SPINLOCK(ath724x_pci_lock); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 22 | |
| 23 | static int ath724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where, |
| 24 | int size, uint32_t *value) |
| 25 | { |
| 26 | unsigned long flags, addr, tval, mask; |
| 27 | |
| 28 | if (devfn) |
| 29 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 30 | |
| 31 | if (where & (size - 1)) |
| 32 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 33 | |
| 34 | spin_lock_irqsave(&ath724x_pci_lock, flags); |
| 35 | |
| 36 | switch (size) { |
| 37 | case 1: |
| 38 | addr = where & ~3; |
| 39 | mask = 0xff000000 >> ((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 2: |
| 45 | addr = where & ~3; |
| 46 | mask = 0xffff0000 >> ((where % 4)*8); |
| 47 | tval = reg_read(ATH724X_PCI_DEV_BASE + addr); |
| 48 | tval = tval & ~mask; |
| 49 | *value = (tval >> ((4 - (where % 4))*8)); |
| 50 | break; |
| 51 | case 4: |
| 52 | *value = reg_read(ATH724X_PCI_DEV_BASE + where); |
| 53 | break; |
| 54 | default: |
| 55 | spin_unlock_irqrestore(&ath724x_pci_lock, flags); |
| 56 | |
| 57 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 58 | } |
| 59 | |
| 60 | spin_unlock_irqrestore(&ath724x_pci_lock, flags); |
| 61 | |
| 62 | return PCIBIOS_SUCCESSFUL; |
| 63 | } |
| 64 | |
| 65 | static int ath724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where, |
| 66 | int size, uint32_t value) |
| 67 | { |
| 68 | unsigned long flags, tval, addr, mask; |
| 69 | |
| 70 | if (devfn) |
| 71 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 72 | |
| 73 | if (where & (size - 1)) |
| 74 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 75 | |
| 76 | spin_lock_irqsave(&ath724x_pci_lock, flags); |
| 77 | |
| 78 | switch (size) { |
| 79 | case 1: |
| 80 | addr = (ATH724X_PCI_DEV_BASE + where) & ~3; |
| 81 | mask = 0xff000000 >> ((where % 4)*8); |
| 82 | tval = reg_read(addr); |
| 83 | tval = tval & ~mask; |
| 84 | tval |= (value << ((4 - (where % 4))*8)) & mask; |
| 85 | reg_write(addr, tval); |
| 86 | break; |
| 87 | case 2: |
| 88 | addr = (ATH724X_PCI_DEV_BASE + where) & ~3; |
| 89 | mask = 0xffff0000 >> ((where % 4)*8); |
| 90 | tval = reg_read(addr); |
| 91 | tval = tval & ~mask; |
| 92 | tval |= (value << ((4 - (where % 4))*8)) & mask; |
| 93 | reg_write(addr, tval); |
| 94 | break; |
| 95 | case 4: |
| 96 | reg_write((ATH724X_PCI_DEV_BASE + where), value); |
| 97 | break; |
| 98 | default: |
| 99 | spin_unlock_irqrestore(&ath724x_pci_lock, flags); |
| 100 | |
| 101 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 102 | } |
| 103 | |
| 104 | spin_unlock_irqrestore(&ath724x_pci_lock, flags); |
| 105 | |
| 106 | return PCIBIOS_SUCCESSFUL; |
| 107 | } |
| 108 | |
| 109 | static struct pci_ops ath724x_pci_ops = { |
| 110 | .read = ath724x_pci_read, |
| 111 | .write = ath724x_pci_write, |
| 112 | }; |
| 113 | |
| 114 | static struct resource ath724x_io_resource = { |
| 115 | .name = "PCI IO space", |
| 116 | .start = 0, |
| 117 | .end = 0, |
| 118 | .flags = IORESOURCE_IO, |
| 119 | }; |
| 120 | |
| 121 | static struct resource ath724x_mem_resource = { |
| 122 | .name = "PCI memory space", |
| 123 | .start = ATH724X_PCI_MEM_BASE, |
| 124 | .end = ATH724X_PCI_MEM_BASE + ATH724X_PCI_MEM_SIZE - 1, |
| 125 | .flags = IORESOURCE_MEM, |
| 126 | }; |
| 127 | |
| 128 | static struct pci_controller ath724x_pci_controller = { |
| 129 | .pci_ops = &ath724x_pci_ops, |
| 130 | .io_resource = &ath724x_io_resource, |
| 131 | .mem_resource = &ath724x_mem_resource, |
| 132 | }; |
| 133 | |
Gabor Juhos | 659243c | 2012-03-14 10:29:23 +0100 | [diff] [blame^] | 134 | int __init ath724x_pcibios_init(void) |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 135 | { |
| 136 | register_pci_controller(&ath724x_pci_controller); |
| 137 | |
| 138 | return PCIBIOS_SUCCESSFUL; |
| 139 | } |
| 140 | |
| 141 | arch_initcall(ath724x_pcibios_init); |