fedc3d7ffa77915e4ac1d1620ce89e705a7a5e23
[pandora-kernel.git] / arch / x86 / platform / mrst / 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
13 #define pr_fmt(fmt) "mrst: " fmt
14
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/interrupt.h>
18 #include <linux/scatterlist.h>
19 #include <linux/sfi.h>
20 #include <linux/intel_pmic_gpio.h>
21 #include <linux/spi/spi.h>
22 #include <linux/i2c.h>
23 #include <linux/i2c/pca953x.h>
24 #include <linux/gpio_keys.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/irq.h>
28 #include <linux/module.h>
29 #include <linux/notifier.h>
30 #include <linux/mfd/intel_msic.h>
31
32 #include <asm/setup.h>
33 #include <asm/mpspec_def.h>
34 #include <asm/hw_irq.h>
35 #include <asm/apic.h>
36 #include <asm/io_apic.h>
37 #include <asm/mrst.h>
38 #include <asm/mrst-vrtc.h>
39 #include <asm/io.h>
40 #include <asm/i8259.h>
41 #include <asm/intel_scu_ipc.h>
42 #include <asm/apb_timer.h>
43 #include <asm/reboot.h>
44
45 /*
46  * the clockevent devices on Moorestown/Medfield can be APBT or LAPIC clock,
47  * cmdline option x86_mrst_timer can be used to override the configuration
48  * to prefer one or the other.
49  * at runtime, there are basically three timer configurations:
50  * 1. per cpu apbt clock only
51  * 2. per cpu always-on lapic clocks only, this is Penwell/Medfield only
52  * 3. per cpu lapic clock (C3STOP) and one apbt clock, with broadcast.
53  *
54  * by default (without cmdline option), platform code first detects cpu type
55  * to see if we are on lincroft or penwell, then set up both lapic or apbt
56  * clocks accordingly.
57  * i.e. by default, medfield uses configuration #2, moorestown uses #1.
58  * config #3 is supported but not recommended on medfield.
59  *
60  * rating and feature summary:
61  * lapic (with C3STOP) --------- 100
62  * apbt (always-on) ------------ 110
63  * lapic (always-on,ARAT) ------ 150
64  */
65
66 __cpuinitdata enum mrst_timer_options mrst_timer_options;
67
68 static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
69 static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
70 enum mrst_cpu_type __mrst_cpu_chip;
71 EXPORT_SYMBOL_GPL(__mrst_cpu_chip);
72
73 int sfi_mtimer_num;
74
75 struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
76 EXPORT_SYMBOL_GPL(sfi_mrtc_array);
77 int sfi_mrtc_num;
78
79 static void mrst_power_off(void)
80 {
81         if (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT)
82                 intel_scu_ipc_simple_command(IPCMSG_COLD_RESET, 1);
83 }
84
85 static void mrst_reboot(void)
86 {
87         if (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT)
88                 intel_scu_ipc_simple_command(IPCMSG_COLD_RESET, 0);
89         else
90                 intel_scu_ipc_simple_command(IPCMSG_COLD_BOOT, 0);
91 }
92
93 /* parse all the mtimer info to a static mtimer array */
94 static int __init sfi_parse_mtmr(struct sfi_table_header *table)
95 {
96         struct sfi_table_simple *sb;
97         struct sfi_timer_table_entry *pentry;
98         struct mpc_intsrc mp_irq;
99         int totallen;
100
101         sb = (struct sfi_table_simple *)table;
102         if (!sfi_mtimer_num) {
103                 sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
104                                         struct sfi_timer_table_entry);
105                 pentry = (struct sfi_timer_table_entry *) sb->pentry;
106                 totallen = sfi_mtimer_num * sizeof(*pentry);
107                 memcpy(sfi_mtimer_array, pentry, totallen);
108         }
109
110         pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
111         pentry = sfi_mtimer_array;
112         for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
113                 pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz,"
114                         " irq = %d\n", totallen, (u32)pentry->phys_addr,
115                         pentry->freq_hz, pentry->irq);
116                         if (!pentry->irq)
117                                 continue;
118                         mp_irq.type = MP_INTSRC;
119                         mp_irq.irqtype = mp_INT;
120 /* triggering mode edge bit 2-3, active high polarity bit 0-1 */
121                         mp_irq.irqflag = 5;
122                         mp_irq.srcbus = MP_BUS_ISA;
123                         mp_irq.srcbusirq = pentry->irq; /* IRQ */
124                         mp_irq.dstapic = MP_APIC_ALL;
125                         mp_irq.dstirq = pentry->irq;
126                         mp_save_irq(&mp_irq);
127         }
128
129         return 0;
130 }
131
132 struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
133 {
134         int i;
135         if (hint < sfi_mtimer_num) {
136                 if (!sfi_mtimer_usage[hint]) {
137                         pr_debug("hint taken for timer %d irq %d\n",\
138                                 hint, sfi_mtimer_array[hint].irq);
139                         sfi_mtimer_usage[hint] = 1;
140                         return &sfi_mtimer_array[hint];
141                 }
142         }
143         /* take the first timer available */
144         for (i = 0; i < sfi_mtimer_num;) {
145                 if (!sfi_mtimer_usage[i]) {
146                         sfi_mtimer_usage[i] = 1;
147                         return &sfi_mtimer_array[i];
148                 }
149                 i++;
150         }
151         return NULL;
152 }
153
154 void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
155 {
156         int i;
157         for (i = 0; i < sfi_mtimer_num;) {
158                 if (mtmr->irq == sfi_mtimer_array[i].irq) {
159                         sfi_mtimer_usage[i] = 0;
160                         return;
161                 }
162                 i++;
163         }
164 }
165
166 /* parse all the mrtc info to a global mrtc array */
167 int __init sfi_parse_mrtc(struct sfi_table_header *table)
168 {
169         struct sfi_table_simple *sb;
170         struct sfi_rtc_table_entry *pentry;
171         struct mpc_intsrc mp_irq;
172
173         int totallen;
174
175         sb = (struct sfi_table_simple *)table;
176         if (!sfi_mrtc_num) {
177                 sfi_mrtc_num = SFI_GET_NUM_ENTRIES(sb,
178                                                 struct sfi_rtc_table_entry);
179                 pentry = (struct sfi_rtc_table_entry *)sb->pentry;
180                 totallen = sfi_mrtc_num * sizeof(*pentry);
181                 memcpy(sfi_mrtc_array, pentry, totallen);
182         }
183
184         pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
185         pentry = sfi_mrtc_array;
186         for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
187                 pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
188                         totallen, (u32)pentry->phys_addr, pentry->irq);
189                 mp_irq.type = MP_INTSRC;
190                 mp_irq.irqtype = mp_INT;
191                 mp_irq.irqflag = 0xf;   /* level trigger and active low */
192                 mp_irq.srcbus = MP_BUS_ISA;
193                 mp_irq.srcbusirq = pentry->irq; /* IRQ */
194                 mp_irq.dstapic = MP_APIC_ALL;
195                 mp_irq.dstirq = pentry->irq;
196                 mp_save_irq(&mp_irq);
197         }
198         return 0;
199 }
200
201 static unsigned long __init mrst_calibrate_tsc(void)
202 {
203         unsigned long flags, fast_calibrate;
204         if (__mrst_cpu_chip == MRST_CPU_CHIP_PENWELL) {
205                 u32 lo, hi, ratio, fsb;
206
207                 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
208                 pr_debug("IA32 perf status is 0x%x, 0x%0x\n", lo, hi);
209                 ratio = (hi >> 8) & 0x1f;
210                 pr_debug("ratio is %d\n", ratio);
211                 if (!ratio) {
212                         pr_err("read a zero ratio, should be incorrect!\n");
213                         pr_err("force tsc ratio to 16 ...\n");
214                         ratio = 16;
215                 }
216                 rdmsr(MSR_FSB_FREQ, lo, hi);
217                 if ((lo & 0x7) == 0x7)
218                         fsb = PENWELL_FSB_FREQ_83SKU;
219                 else
220                         fsb = PENWELL_FSB_FREQ_100SKU;
221                 fast_calibrate = ratio * fsb;
222                 pr_debug("read penwell tsc %lu khz\n", fast_calibrate);
223                 lapic_timer_frequency = fsb * 1000 / HZ;
224                 /* mark tsc clocksource as reliable */
225                 set_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC_RELIABLE);
226         } else {
227                 local_irq_save(flags);
228                 fast_calibrate = apbt_quick_calibrate();
229                 local_irq_restore(flags);
230         }
231         
232         if (fast_calibrate)
233                 return fast_calibrate;
234
235         return 0;
236 }
237
238 static void __init mrst_time_init(void)
239 {
240         sfi_table_parse(SFI_SIG_MTMR, NULL, NULL, sfi_parse_mtmr);
241         switch (mrst_timer_options) {
242         case MRST_TIMER_APBT_ONLY:
243                 break;
244         case MRST_TIMER_LAPIC_APBT:
245                 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
246                 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
247                 break;
248         default:
249                 if (!boot_cpu_has(X86_FEATURE_ARAT))
250                         break;
251                 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
252                 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
253                 return;
254         }
255         /* we need at least one APB timer */
256         pre_init_apic_IRQ0();
257         apbt_time_init();
258 }
259
260 static void __cpuinit mrst_arch_setup(void)
261 {
262         if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x27)
263                 __mrst_cpu_chip = MRST_CPU_CHIP_PENWELL;
264         else if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x26)
265                 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
266         else {
267                 pr_err("Unknown Moorestown CPU (%d:%d), default to Lincroft\n",
268                         boot_cpu_data.x86, boot_cpu_data.x86_model);
269                 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
270         }
271         pr_debug("Moorestown CPU %s identified\n",
272                 (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT) ?
273                 "Lincroft" : "Penwell");
274 }
275
276 /* MID systems don't have i8042 controller */
277 static int mrst_i8042_detect(void)
278 {
279         return 0;
280 }
281
282 /*
283  * Moorestown does not have external NMI source nor port 0x61 to report
284  * NMI status. The possible NMI sources are from pmu as a result of NMI
285  * watchdog or lock debug. Reading io port 0x61 results in 0xff which
286  * misled NMI handler.
287  */
288 static unsigned char mrst_get_nmi_reason(void)
289 {
290         return 0;
291 }
292
293 /*
294  * Moorestown specific x86_init function overrides and early setup
295  * calls.
296  */
297 void __init x86_mrst_early_setup(void)
298 {
299         x86_init.resources.probe_roms = x86_init_noop;
300         x86_init.resources.reserve_resources = x86_init_noop;
301
302         x86_init.timers.timer_init = mrst_time_init;
303         x86_init.timers.setup_percpu_clockev = x86_init_noop;
304
305         x86_init.irqs.pre_vector_init = x86_init_noop;
306
307         x86_init.oem.arch_setup = mrst_arch_setup;
308
309         x86_cpuinit.setup_percpu_clockev = apbt_setup_secondary_clock;
310
311         x86_platform.calibrate_tsc = mrst_calibrate_tsc;
312         x86_platform.i8042_detect = mrst_i8042_detect;
313         x86_init.timers.wallclock_init = mrst_rtc_init;
314         x86_platform.get_nmi_reason = mrst_get_nmi_reason;
315
316         x86_init.pci.init = pci_mrst_init;
317         x86_init.pci.fixup_irqs = x86_init_noop;
318
319         legacy_pic = &null_legacy_pic;
320
321         /* Moorestown specific power_off/restart method */
322         pm_power_off = mrst_power_off;
323         machine_ops.emergency_restart  = mrst_reboot;
324
325         /* Avoid searching for BIOS MP tables */
326         x86_init.mpparse.find_smp_config = x86_init_noop;
327         x86_init.mpparse.get_smp_config = x86_init_uint_noop;
328         set_bit(MP_BUS_ISA, mp_bus_not_pci);
329 }
330
331 /*
332  * if user does not want to use per CPU apb timer, just give it a lower rating
333  * than local apic timer and skip the late per cpu timer init.
334  */
335 static inline int __init setup_x86_mrst_timer(char *arg)
336 {
337         if (!arg)
338                 return -EINVAL;
339
340         if (strcmp("apbt_only", arg) == 0)
341                 mrst_timer_options = MRST_TIMER_APBT_ONLY;
342         else if (strcmp("lapic_and_apbt", arg) == 0)
343                 mrst_timer_options = MRST_TIMER_LAPIC_APBT;
344         else {
345                 pr_warning("X86 MRST timer option %s not recognised"
346                            " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
347                            arg);
348                 return -EINVAL;
349         }
350         return 0;
351 }
352 __setup("x86_mrst_timer=", setup_x86_mrst_timer);
353
354 /*
355  * Parsing GPIO table first, since the DEVS table will need this table
356  * to map the pin name to the actual pin.
357  */
358 static struct sfi_gpio_table_entry *gpio_table;
359 static int gpio_num_entry;
360
361 static int __init sfi_parse_gpio(struct sfi_table_header *table)
362 {
363         struct sfi_table_simple *sb;
364         struct sfi_gpio_table_entry *pentry;
365         int num, i;
366
367         if (gpio_table)
368                 return 0;
369         sb = (struct sfi_table_simple *)table;
370         num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
371         pentry = (struct sfi_gpio_table_entry *)sb->pentry;
372
373         gpio_table = (struct sfi_gpio_table_entry *)
374                                 kmalloc(num * sizeof(*pentry), GFP_KERNEL);
375         if (!gpio_table)
376                 return -1;
377         memcpy(gpio_table, pentry, num * sizeof(*pentry));
378         gpio_num_entry = num;
379
380         pr_debug("GPIO pin info:\n");
381         for (i = 0; i < num; i++, pentry++)
382                 pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
383                 " pin = %d\n", i,
384                         pentry->controller_name,
385                         pentry->pin_name,
386                         pentry->pin_no);
387         return 0;
388 }
389
390 static int get_gpio_by_name(const char *name)
391 {
392         struct sfi_gpio_table_entry *pentry = gpio_table;
393         int i;
394
395         if (!pentry)
396                 return -1;
397         for (i = 0; i < gpio_num_entry; i++, pentry++) {
398                 if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
399                         return pentry->pin_no;
400         }
401         return -1;
402 }
403
404 /*
405  * Here defines the array of devices platform data that IAFW would export
406  * through SFI "DEVS" table, we use name and type to match the device and
407  * its platform data.
408  */
409 struct devs_id {
410         char name[SFI_NAME_LEN + 1];
411         u8 type;
412         u8 delay;
413         void *(*get_platform_data)(void *info);
414 };
415
416 /* the offset for the mapping of global gpio pin to irq */
417 #define MRST_IRQ_OFFSET 0x100
418
419 static void __init *pmic_gpio_platform_data(void *info)
420 {
421         static struct intel_pmic_gpio_platform_data pmic_gpio_pdata;
422         int gpio_base = get_gpio_by_name("pmic_gpio_base");
423
424         if (gpio_base == -1)
425                 gpio_base = 64;
426         pmic_gpio_pdata.gpio_base = gpio_base;
427         pmic_gpio_pdata.irq_base = gpio_base + MRST_IRQ_OFFSET;
428         pmic_gpio_pdata.gpiointr = 0xffffeff8;
429
430         return &pmic_gpio_pdata;
431 }
432
433 static void __init *max3111_platform_data(void *info)
434 {
435         struct spi_board_info *spi_info = info;
436         int intr = get_gpio_by_name("max3111_int");
437
438         spi_info->mode = SPI_MODE_0;
439         if (intr == -1)
440                 return NULL;
441         spi_info->irq = intr + MRST_IRQ_OFFSET;
442         return NULL;
443 }
444
445 /* we have multiple max7315 on the board ... */
446 #define MAX7315_NUM 2
447 static void __init *max7315_platform_data(void *info)
448 {
449         static struct pca953x_platform_data max7315_pdata[MAX7315_NUM];
450         static int nr;
451         struct pca953x_platform_data *max7315 = &max7315_pdata[nr];
452         struct i2c_board_info *i2c_info = info;
453         int gpio_base, intr;
454         char base_pin_name[SFI_NAME_LEN + 1];
455         char intr_pin_name[SFI_NAME_LEN + 1];
456
457         if (nr == MAX7315_NUM) {
458                 pr_err("too many max7315s, we only support %d\n",
459                                 MAX7315_NUM);
460                 return NULL;
461         }
462         /* we have several max7315 on the board, we only need load several
463          * instances of the same pca953x driver to cover them
464          */
465         strcpy(i2c_info->type, "max7315");
466         if (nr++) {
467                 sprintf(base_pin_name, "max7315_%d_base", nr);
468                 sprintf(intr_pin_name, "max7315_%d_int", nr);
469         } else {
470                 strcpy(base_pin_name, "max7315_base");
471                 strcpy(intr_pin_name, "max7315_int");
472         }
473
474         gpio_base = get_gpio_by_name(base_pin_name);
475         intr = get_gpio_by_name(intr_pin_name);
476
477         if (gpio_base == -1)
478                 return NULL;
479         max7315->gpio_base = gpio_base;
480         if (intr != -1) {
481                 i2c_info->irq = intr + MRST_IRQ_OFFSET;
482                 max7315->irq_base = gpio_base + MRST_IRQ_OFFSET;
483         } else {
484                 i2c_info->irq = -1;
485                 max7315->irq_base = -1;
486         }
487         return max7315;
488 }
489
490 static void __init *emc1403_platform_data(void *info)
491 {
492         static short intr2nd_pdata;
493         struct i2c_board_info *i2c_info = info;
494         int intr = get_gpio_by_name("thermal_int");
495         int intr2nd = get_gpio_by_name("thermal_alert");
496
497         if (intr == -1 || intr2nd == -1)
498                 return NULL;
499
500         i2c_info->irq = intr + MRST_IRQ_OFFSET;
501         intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
502
503         return &intr2nd_pdata;
504 }
505
506 static void __init *lis331dl_platform_data(void *info)
507 {
508         static short intr2nd_pdata;
509         struct i2c_board_info *i2c_info = info;
510         int intr = get_gpio_by_name("accel_int");
511         int intr2nd = get_gpio_by_name("accel_2");
512
513         if (intr == -1 || intr2nd == -1)
514                 return NULL;
515
516         i2c_info->irq = intr + MRST_IRQ_OFFSET;
517         intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
518
519         return &intr2nd_pdata;
520 }
521
522 static void __init *no_platform_data(void *info)
523 {
524         return NULL;
525 }
526
527 static struct resource msic_resources[] = {
528         {
529                 .start  = INTEL_MSIC_IRQ_PHYS_BASE,
530                 .end    = INTEL_MSIC_IRQ_PHYS_BASE + 64 - 1,
531                 .flags  = IORESOURCE_MEM,
532         },
533 };
534
535 static struct intel_msic_platform_data msic_pdata;
536
537 static struct platform_device msic_device = {
538         .name           = "intel_msic",
539         .id             = -1,
540         .dev            = {
541                 .platform_data  = &msic_pdata,
542         },
543         .num_resources  = ARRAY_SIZE(msic_resources),
544         .resource       = msic_resources,
545 };
546
547 static inline bool mrst_has_msic(void)
548 {
549         return mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL;
550 }
551
552 static int msic_scu_status_change(struct notifier_block *nb,
553                                   unsigned long code, void *data)
554 {
555         if (code == SCU_DOWN) {
556                 platform_device_unregister(&msic_device);
557                 return 0;
558         }
559
560         return platform_device_register(&msic_device);
561 }
562
563 static int __init msic_init(void)
564 {
565         static struct notifier_block msic_scu_notifier = {
566                 .notifier_call  = msic_scu_status_change,
567         };
568
569         /*
570          * We need to be sure that the SCU IPC is ready before MSIC device
571          * can be registered.
572          */
573         if (mrst_has_msic())
574                 intel_scu_notifier_add(&msic_scu_notifier);
575
576         return 0;
577 }
578 arch_initcall(msic_init);
579
580 /*
581  * msic_generic_platform_data - sets generic platform data for the block
582  * @info: pointer to the SFI device table entry for this block
583  * @block: MSIC block
584  *
585  * Function sets IRQ number from the SFI table entry for given device to
586  * the MSIC platform data.
587  */
588 static void *msic_generic_platform_data(void *info, enum intel_msic_block block)
589 {
590         struct sfi_device_table_entry *entry = info;
591
592         BUG_ON(block < 0 || block >= INTEL_MSIC_BLOCK_LAST);
593         msic_pdata.irq[block] = entry->irq;
594
595         return no_platform_data(info);
596 }
597
598 static void *msic_battery_platform_data(void *info)
599 {
600         return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_BATTERY);
601 }
602
603 static void *msic_gpio_platform_data(void *info)
604 {
605         static struct intel_msic_gpio_pdata pdata;
606         int gpio = get_gpio_by_name("msic_gpio_base");
607
608         if (gpio < 0)
609                 return NULL;
610
611         pdata.gpio_base = gpio;
612         msic_pdata.gpio = &pdata;
613
614         return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_GPIO);
615 }
616
617 static void *msic_audio_platform_data(void *info)
618 {
619         struct platform_device *pdev;
620
621         pdev = platform_device_register_simple("sst-platform", -1, NULL, 0);
622         if (IS_ERR(pdev)) {
623                 pr_err("failed to create audio platform device\n");
624                 return NULL;
625         }
626
627         return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_AUDIO);
628 }
629
630 static void *msic_power_btn_platform_data(void *info)
631 {
632         return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_POWER_BTN);
633 }
634
635 static void *msic_ocd_platform_data(void *info)
636 {
637         static struct intel_msic_ocd_pdata pdata;
638         int gpio = get_gpio_by_name("ocd_gpio");
639
640         if (gpio < 0)
641                 return NULL;
642
643         pdata.gpio = gpio;
644         msic_pdata.ocd = &pdata;
645
646         return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_OCD);
647 }
648
649 static const struct devs_id __initconst device_ids[] = {
650         {"bma023", SFI_DEV_TYPE_I2C, 1, &no_platform_data},
651         {"pmic_gpio", SFI_DEV_TYPE_SPI, 1, &pmic_gpio_platform_data},
652         {"spi_max3111", SFI_DEV_TYPE_SPI, 0, &max3111_platform_data},
653         {"i2c_max7315", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
654         {"i2c_max7315_2", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
655         {"emc1403", SFI_DEV_TYPE_I2C, 1, &emc1403_platform_data},
656         {"i2c_accel", SFI_DEV_TYPE_I2C, 0, &lis331dl_platform_data},
657         {"pmic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
658
659         /* MSIC subdevices */
660         {"msic_battery", SFI_DEV_TYPE_IPC, 1, &msic_battery_platform_data},
661         {"msic_gpio", SFI_DEV_TYPE_IPC, 1, &msic_gpio_platform_data},
662         {"msic_audio", SFI_DEV_TYPE_IPC, 1, &msic_audio_platform_data},
663         {"msic_power_btn", SFI_DEV_TYPE_IPC, 1, &msic_power_btn_platform_data},
664         {"msic_ocd", SFI_DEV_TYPE_IPC, 1, &msic_ocd_platform_data},
665
666         {},
667 };
668
669 #define MAX_IPCDEVS     24
670 static struct platform_device *ipc_devs[MAX_IPCDEVS];
671 static int ipc_next_dev;
672
673 #define MAX_SCU_SPI     24
674 static struct spi_board_info *spi_devs[MAX_SCU_SPI];
675 static int spi_next_dev;
676
677 #define MAX_SCU_I2C     24
678 static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
679 static int i2c_bus[MAX_SCU_I2C];
680 static int i2c_next_dev;
681
682 static void __init intel_scu_device_register(struct platform_device *pdev)
683 {
684         if(ipc_next_dev == MAX_IPCDEVS)
685                 pr_err("too many SCU IPC devices");
686         else
687                 ipc_devs[ipc_next_dev++] = pdev;
688 }
689
690 static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
691 {
692         struct spi_board_info *new_dev;
693
694         if (spi_next_dev == MAX_SCU_SPI) {
695                 pr_err("too many SCU SPI devices");
696                 return;
697         }
698
699         new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
700         if (!new_dev) {
701                 pr_err("failed to alloc mem for delayed spi dev %s\n",
702                         sdev->modalias);
703                 return;
704         }
705         memcpy(new_dev, sdev, sizeof(*sdev));
706
707         spi_devs[spi_next_dev++] = new_dev;
708 }
709
710 static void __init intel_scu_i2c_device_register(int bus,
711                                                 struct i2c_board_info *idev)
712 {
713         struct i2c_board_info *new_dev;
714
715         if (i2c_next_dev == MAX_SCU_I2C) {
716                 pr_err("too many SCU I2C devices");
717                 return;
718         }
719
720         new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
721         if (!new_dev) {
722                 pr_err("failed to alloc mem for delayed i2c dev %s\n",
723                         idev->type);
724                 return;
725         }
726         memcpy(new_dev, idev, sizeof(*idev));
727
728         i2c_bus[i2c_next_dev] = bus;
729         i2c_devs[i2c_next_dev++] = new_dev;
730 }
731
732 BLOCKING_NOTIFIER_HEAD(intel_scu_notifier);
733 EXPORT_SYMBOL_GPL(intel_scu_notifier);
734
735 /* Called by IPC driver */
736 void intel_scu_devices_create(void)
737 {
738         int i;
739
740         for (i = 0; i < ipc_next_dev; i++)
741                 platform_device_add(ipc_devs[i]);
742
743         for (i = 0; i < spi_next_dev; i++)
744                 spi_register_board_info(spi_devs[i], 1);
745
746         for (i = 0; i < i2c_next_dev; i++) {
747                 struct i2c_adapter *adapter;
748                 struct i2c_client *client;
749
750                 adapter = i2c_get_adapter(i2c_bus[i]);
751                 if (adapter) {
752                         client = i2c_new_device(adapter, i2c_devs[i]);
753                         if (!client)
754                                 pr_err("can't create i2c device %s\n",
755                                         i2c_devs[i]->type);
756                 } else
757                         i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
758         }
759         intel_scu_notifier_post(SCU_AVAILABLE, 0L);
760 }
761 EXPORT_SYMBOL_GPL(intel_scu_devices_create);
762
763 /* Called by IPC driver */
764 void intel_scu_devices_destroy(void)
765 {
766         int i;
767
768         intel_scu_notifier_post(SCU_DOWN, 0L);
769
770         for (i = 0; i < ipc_next_dev; i++)
771                 platform_device_del(ipc_devs[i]);
772 }
773 EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
774
775 static void __init install_irq_resource(struct platform_device *pdev, int irq)
776 {
777         /* Single threaded */
778         static struct resource __initdata res = {
779                 .name = "IRQ",
780                 .flags = IORESOURCE_IRQ,
781         };
782         res.start = irq;
783         platform_device_add_resources(pdev, &res, 1);
784 }
785
786 static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *entry)
787 {
788         const struct devs_id *dev = device_ids;
789         struct platform_device *pdev;
790         void *pdata = NULL;
791
792         while (dev->name[0]) {
793                 if (dev->type == SFI_DEV_TYPE_IPC &&
794                         !strncmp(dev->name, entry->name, SFI_NAME_LEN)) {
795                         pdata = dev->get_platform_data(entry);
796                         break;
797                 }
798                 dev++;
799         }
800
801         /*
802          * On Medfield the platform device creation is handled by the MSIC
803          * MFD driver so we don't need to do it here.
804          */
805         if (mrst_has_msic())
806                 return;
807
808         /* ID as IRQ is a hack that will go away */
809         pdev = platform_device_alloc(entry->name, entry->irq);
810         if (pdev == NULL) {
811                 pr_err("out of memory for SFI platform device '%s'.\n",
812                         entry->name);
813                 return;
814         }
815         install_irq_resource(pdev, entry->irq);
816
817         pdev->dev.platform_data = pdata;
818         intel_scu_device_register(pdev);
819 }
820
821 static void __init sfi_handle_spi_dev(struct spi_board_info *spi_info)
822 {
823         const struct devs_id *dev = device_ids;
824         void *pdata = NULL;
825
826         while (dev->name[0]) {
827                 if (dev->type == SFI_DEV_TYPE_SPI &&
828                                 !strncmp(dev->name, spi_info->modalias, SFI_NAME_LEN)) {
829                         pdata = dev->get_platform_data(spi_info);
830                         break;
831                 }
832                 dev++;
833         }
834         spi_info->platform_data = pdata;
835         if (dev->delay)
836                 intel_scu_spi_device_register(spi_info);
837         else
838                 spi_register_board_info(spi_info, 1);
839 }
840
841 static void __init sfi_handle_i2c_dev(int bus, struct i2c_board_info *i2c_info)
842 {
843         const struct devs_id *dev = device_ids;
844         void *pdata = NULL;
845
846         while (dev->name[0]) {
847                 if (dev->type == SFI_DEV_TYPE_I2C &&
848                         !strncmp(dev->name, i2c_info->type, SFI_NAME_LEN)) {
849                         pdata = dev->get_platform_data(i2c_info);
850                         break;
851                 }
852                 dev++;
853         }
854         i2c_info->platform_data = pdata;
855
856         if (dev->delay)
857                 intel_scu_i2c_device_register(bus, i2c_info);
858         else
859                 i2c_register_board_info(bus, i2c_info, 1);
860  }
861
862
863 static int __init sfi_parse_devs(struct sfi_table_header *table)
864 {
865         struct sfi_table_simple *sb;
866         struct sfi_device_table_entry *pentry;
867         struct spi_board_info spi_info;
868         struct i2c_board_info i2c_info;
869         int num, i, bus;
870         int ioapic;
871         struct io_apic_irq_attr irq_attr;
872
873         sb = (struct sfi_table_simple *)table;
874         num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
875         pentry = (struct sfi_device_table_entry *)sb->pentry;
876
877         for (i = 0; i < num; i++, pentry++) {
878                 int irq = pentry->irq;
879
880                 if (irq != (u8)0xff) { /* native RTE case */
881                         /* these SPI2 devices are not exposed to system as PCI
882                          * devices, but they have separate RTE entry in IOAPIC
883                          * so we have to enable them one by one here
884                          */
885                         ioapic = mp_find_ioapic(irq);
886                         irq_attr.ioapic = ioapic;
887                         irq_attr.ioapic_pin = irq;
888                         irq_attr.trigger = 1;
889                         irq_attr.polarity = 1;
890                         io_apic_set_pci_routing(NULL, irq, &irq_attr);
891                 } else
892                         irq = 0; /* No irq */
893
894                 switch (pentry->type) {
895                 case SFI_DEV_TYPE_IPC:
896                         pr_debug("info[%2d]: IPC bus, name = %16.16s, "
897                                 "irq = 0x%2x\n", i, pentry->name, pentry->irq);
898                         sfi_handle_ipc_dev(pentry);
899                         break;
900                 case SFI_DEV_TYPE_SPI:
901                         memset(&spi_info, 0, sizeof(spi_info));
902                         strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
903                         spi_info.irq = irq;
904                         spi_info.bus_num = pentry->host_num;
905                         spi_info.chip_select = pentry->addr;
906                         spi_info.max_speed_hz = pentry->max_freq;
907                         pr_debug("info[%2d]: SPI bus = %d, name = %16.16s, "
908                                 "irq = 0x%2x, max_freq = %d, cs = %d\n", i,
909                                 spi_info.bus_num,
910                                 spi_info.modalias,
911                                 spi_info.irq,
912                                 spi_info.max_speed_hz,
913                                 spi_info.chip_select);
914                         sfi_handle_spi_dev(&spi_info);
915                         break;
916                 case SFI_DEV_TYPE_I2C:
917                         memset(&i2c_info, 0, sizeof(i2c_info));
918                         bus = pentry->host_num;
919                         strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
920                         i2c_info.irq = irq;
921                         i2c_info.addr = pentry->addr;
922                         pr_debug("info[%2d]: I2C bus = %d, name = %16.16s, "
923                                 "irq = 0x%2x, addr = 0x%x\n", i, bus,
924                                 i2c_info.type,
925                                 i2c_info.irq,
926                                 i2c_info.addr);
927                         sfi_handle_i2c_dev(bus, &i2c_info);
928                         break;
929                 case SFI_DEV_TYPE_UART:
930                 case SFI_DEV_TYPE_HSI:
931                 default:
932                         ;
933                 }
934         }
935         return 0;
936 }
937
938 static int __init mrst_platform_init(void)
939 {
940         sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
941         sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
942         return 0;
943 }
944 arch_initcall(mrst_platform_init);
945
946 /*
947  * we will search these buttons in SFI GPIO table (by name)
948  * and register them dynamically. Please add all possible
949  * buttons here, we will shrink them if no GPIO found.
950  */
951 static struct gpio_keys_button gpio_button[] = {
952         {KEY_POWER,             -1, 1, "power_btn",     EV_KEY, 0, 3000},
953         {KEY_PROG1,             -1, 1, "prog_btn1",     EV_KEY, 0, 20},
954         {KEY_PROG2,             -1, 1, "prog_btn2",     EV_KEY, 0, 20},
955         {SW_LID,                -1, 1, "lid_switch",    EV_SW,  0, 20},
956         {KEY_VOLUMEUP,          -1, 1, "vol_up",        EV_KEY, 0, 20},
957         {KEY_VOLUMEDOWN,        -1, 1, "vol_down",      EV_KEY, 0, 20},
958         {KEY_CAMERA,            -1, 1, "camera_full",   EV_KEY, 0, 20},
959         {KEY_CAMERA_FOCUS,      -1, 1, "camera_half",   EV_KEY, 0, 20},
960         {SW_KEYPAD_SLIDE,       -1, 1, "MagSw1",        EV_SW,  0, 20},
961         {SW_KEYPAD_SLIDE,       -1, 1, "MagSw2",        EV_SW,  0, 20},
962 };
963
964 static struct gpio_keys_platform_data mrst_gpio_keys = {
965         .buttons        = gpio_button,
966         .rep            = 1,
967         .nbuttons       = -1, /* will fill it after search */
968 };
969
970 static struct platform_device pb_device = {
971         .name           = "gpio-keys",
972         .id             = -1,
973         .dev            = {
974                 .platform_data  = &mrst_gpio_keys,
975         },
976 };
977
978 /*
979  * Shrink the non-existent buttons, register the gpio button
980  * device if there is some
981  */
982 static int __init pb_keys_init(void)
983 {
984         struct gpio_keys_button *gb = gpio_button;
985         int i, num, good = 0;
986
987         num = sizeof(gpio_button) / sizeof(struct gpio_keys_button);
988         for (i = 0; i < num; i++) {
989                 gb[i].gpio = get_gpio_by_name(gb[i].desc);
990                 if (gb[i].gpio == -1)
991                         continue;
992
993                 if (i != good)
994                         gb[good] = gb[i];
995                 good++;
996         }
997
998         if (good) {
999                 mrst_gpio_keys.nbuttons = good;
1000                 return platform_device_register(&pb_device);
1001         }
1002         return 0;
1003 }
1004 late_initcall(pb_keys_init);