b7fa049c826cbc2b4d319559a5774d517945c1de
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / mrst.c
1 /*
2  * mrst.c: Intel Moorestown platform specific setup code
3  *
4  * (C) Copyright 2008 Intel Corporation
5  * Author: Jacob Pan (jacob.jun.pan@intel.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; version 2
10  * of the License.
11  */
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/sfi.h>
15 #include <linux/irq.h>
16 #include <linux/module.h>
17
18 #include <asm/setup.h>
19 #include <asm/mpspec_def.h>
20 #include <asm/hw_irq.h>
21 #include <asm/apic.h>
22 #include <asm/io_apic.h>
23 #include <asm/mrst.h>
24 #include <asm/io.h>
25 #include <asm/i8259.h>
26
27 static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
28 static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
29 int sfi_mtimer_num;
30
31 struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
32 EXPORT_SYMBOL_GPL(sfi_mrtc_array);
33 int sfi_mrtc_num;
34
35 static inline void assign_to_mp_irq(struct mpc_intsrc *m,
36                                     struct mpc_intsrc *mp_irq)
37 {
38         memcpy(mp_irq, m, sizeof(struct mpc_intsrc));
39 }
40
41 static inline int mp_irq_cmp(struct mpc_intsrc *mp_irq,
42                                 struct mpc_intsrc *m)
43 {
44         return memcmp(mp_irq, m, sizeof(struct mpc_intsrc));
45 }
46
47 static void save_mp_irq(struct mpc_intsrc *m)
48 {
49         int i;
50
51         for (i = 0; i < mp_irq_entries; i++) {
52                 if (!mp_irq_cmp(&mp_irqs[i], m))
53                         return;
54         }
55
56         assign_to_mp_irq(m, &mp_irqs[mp_irq_entries]);
57         if (++mp_irq_entries == MAX_IRQ_SOURCES)
58                 panic("Max # of irq sources exceeded!!\n");
59 }
60
61 /* parse all the mtimer info to a static mtimer array */
62 static int __init sfi_parse_mtmr(struct sfi_table_header *table)
63 {
64         struct sfi_table_simple *sb;
65         struct sfi_timer_table_entry *pentry;
66         struct mpc_intsrc mp_irq;
67         int totallen;
68
69         sb = (struct sfi_table_simple *)table;
70         if (!sfi_mtimer_num) {
71                 sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
72                                         struct sfi_timer_table_entry);
73                 pentry = (struct sfi_timer_table_entry *) sb->pentry;
74                 totallen = sfi_mtimer_num * sizeof(*pentry);
75                 memcpy(sfi_mtimer_array, pentry, totallen);
76         }
77
78         printk(KERN_INFO "SFI: MTIMER info (num = %d):\n", sfi_mtimer_num);
79         pentry = sfi_mtimer_array;
80         for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
81                 printk(KERN_INFO "timer[%d]: paddr = 0x%08x, freq = %dHz,"
82                         " irq = %d\n", totallen, (u32)pentry->phys_addr,
83                         pentry->freq_hz, pentry->irq);
84                         if (!pentry->irq)
85                                 continue;
86                         mp_irq.type = MP_IOAPIC;
87                         mp_irq.irqtype = mp_INT;
88 /* triggering mode edge bit 2-3, active high polarity bit 0-1 */
89                         mp_irq.irqflag = 5;
90                         mp_irq.srcbus = 0;
91                         mp_irq.srcbusirq = pentry->irq; /* IRQ */
92                         mp_irq.dstapic = MP_APIC_ALL;
93                         mp_irq.dstirq = pentry->irq;
94                         save_mp_irq(&mp_irq);
95         }
96
97         return 0;
98 }
99
100 struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
101 {
102         int i;
103         if (hint < sfi_mtimer_num) {
104                 if (!sfi_mtimer_usage[hint]) {
105                         pr_debug("hint taken for timer %d irq %d\n",\
106                                 hint, sfi_mtimer_array[hint].irq);
107                         sfi_mtimer_usage[hint] = 1;
108                         return &sfi_mtimer_array[hint];
109                 }
110         }
111         /* take the first timer available */
112         for (i = 0; i < sfi_mtimer_num;) {
113                 if (!sfi_mtimer_usage[i]) {
114                         sfi_mtimer_usage[i] = 1;
115                         return &sfi_mtimer_array[i];
116                 }
117                 i++;
118         }
119         return NULL;
120 }
121
122 void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
123 {
124         int i;
125         for (i = 0; i < sfi_mtimer_num;) {
126                 if (mtmr->irq == sfi_mtimer_array[i].irq) {
127                         sfi_mtimer_usage[i] = 0;
128                         return;
129                 }
130                 i++;
131         }
132 }
133
134 /* parse all the mrtc info to a global mrtc array */
135 int __init sfi_parse_mrtc(struct sfi_table_header *table)
136 {
137         struct sfi_table_simple *sb;
138         struct sfi_rtc_table_entry *pentry;
139         struct mpc_intsrc mp_irq;
140
141         int totallen;
142
143         sb = (struct sfi_table_simple *)table;
144         if (!sfi_mrtc_num) {
145                 sfi_mrtc_num = SFI_GET_NUM_ENTRIES(sb,
146                                                 struct sfi_rtc_table_entry);
147                 pentry = (struct sfi_rtc_table_entry *)sb->pentry;
148                 totallen = sfi_mrtc_num * sizeof(*pentry);
149                 memcpy(sfi_mrtc_array, pentry, totallen);
150         }
151
152         printk(KERN_INFO "SFI: RTC info (num = %d):\n", sfi_mrtc_num);
153         pentry = sfi_mrtc_array;
154         for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
155                 printk(KERN_INFO "RTC[%d]: paddr = 0x%08x, irq = %d\n",
156                         totallen, (u32)pentry->phys_addr, pentry->irq);
157                 mp_irq.type = MP_IOAPIC;
158                 mp_irq.irqtype = mp_INT;
159                 mp_irq.irqflag = 0;
160                 mp_irq.srcbus = 0;
161                 mp_irq.srcbusirq = pentry->irq; /* IRQ */
162                 mp_irq.dstapic = MP_APIC_ALL;
163                 mp_irq.dstirq = pentry->irq;
164                 save_mp_irq(&mp_irq);
165         }
166         return 0;
167 }
168
169 void __init mrst_rtc_init(void)
170 {
171         sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
172 }
173
174 /*
175  * Moorestown specific x86_init function overrides and early setup
176  * calls.
177  */
178 void __init x86_mrst_early_setup(void)
179 {
180         x86_init.resources.probe_roms = x86_init_noop;
181         x86_init.resources.reserve_resources = x86_init_noop;
182
183         x86_init.pci.init = pci_mrst_init;
184         x86_init.pci.fixup_irqs = x86_init_noop;
185
186         legacy_pic = &null_legacy_pic;
187 }