blob: ad4ec1cb097ecfae17e4a99aed37cf985b03cb78 [file] [log] [blame]
Thomas Gleixner3f4110a2009-08-29 14:54:20 +02001/*
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 */
Feng Tang1da4b1c2010-11-09 11:22:58 +000012
13#define pr_fmt(fmt) "mrst: " fmt
14
Thomas Gleixner3f4110a2009-08-29 14:54:20 +020015#include <linux/init.h>
Jacob Pan16ab5392010-02-12 03:08:30 -080016#include <linux/kernel.h>
Feng Tangefe3ed92011-08-26 11:25:14 +010017#include <linux/interrupt.h>
18#include <linux/scatterlist.h>
Jacob Pan16ab5392010-02-12 03:08:30 -080019#include <linux/sfi.h>
Feng Tang1da4b1c2010-11-09 11:22:58 +000020#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>
Jacob Pan16ab5392010-02-12 03:08:30 -080027#include <linux/irq.h>
Feng Tangcf089452010-02-12 03:37:38 -080028#include <linux/module.h>
Alan Cox42c25442011-09-07 16:06:51 +030029#include <linux/notifier.h>
Mika Westerberg360545c2011-10-18 12:41:22 +030030#include <linux/mfd/intel_msic.h>
Thomas Gleixner3f4110a2009-08-29 14:54:20 +020031
32#include <asm/setup.h>
Jacob Pan16ab5392010-02-12 03:08:30 -080033#include <asm/mpspec_def.h>
34#include <asm/hw_irq.h>
35#include <asm/apic.h>
36#include <asm/io_apic.h>
Jacob Pan5b78b672010-02-12 02:29:11 -080037#include <asm/mrst.h>
Feng Tang168202c2011-02-15 00:13:32 +080038#include <asm/mrst-vrtc.h>
Jacob Pan5b78b672010-02-12 02:29:11 -080039#include <asm/io.h>
40#include <asm/i8259.h>
Feng Tang1da4b1c2010-11-09 11:22:58 +000041#include <asm/intel_scu_ipc.h>
Jacob Pan3746c6b2010-02-12 05:01:12 -080042#include <asm/apb_timer.h>
Alek Ducfb505a2010-11-10 16:50:08 +000043#include <asm/reboot.h>
Thomas Gleixner3f4110a2009-08-29 14:54:20 +020044
Jacob Pana875c012010-05-19 12:01:25 -070045/*
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
H. Peter Anvin14671382010-05-19 14:37:40 -070066__cpuinitdata enum mrst_timer_options mrst_timer_options;
Jacob Pana875c012010-05-19 12:01:25 -070067
Jacob Pan16ab5392010-02-12 03:08:30 -080068static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
69static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
H. Peter Anvina75af582010-05-19 13:40:14 -070070enum mrst_cpu_type __mrst_cpu_chip;
71EXPORT_SYMBOL_GPL(__mrst_cpu_chip);
Jacob Pana0c173b2010-05-19 12:01:24 -070072
Jacob Pan16ab5392010-02-12 03:08:30 -080073int sfi_mtimer_num;
74
Feng Tangcf089452010-02-12 03:37:38 -080075struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
76EXPORT_SYMBOL_GPL(sfi_mrtc_array);
77int sfi_mrtc_num;
78
Jacob Pan48bc5562011-11-16 16:07:22 +000079static 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
85static 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
Jacob Pan16ab5392010-02-12 03:08:30 -080093/* parse all the mtimer info to a static mtimer array */
94static 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
Feng Tang1da4b1c2010-11-09 11:22:58 +0000110 pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
Jacob Pan16ab5392010-02-12 03:08:30 -0800111 pentry = sfi_mtimer_array;
112 for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
Feng Tang1da4b1c2010-11-09 11:22:58 +0000113 pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz,"
Jacob Pan16ab5392010-02-12 03:08:30 -0800114 " irq = %d\n", totallen, (u32)pentry->phys_addr,
115 pentry->freq_hz, pentry->irq);
116 if (!pentry->irq)
117 continue;
Jacob Pan9d90e492011-04-08 11:23:00 -0700118 mp_irq.type = MP_INTSRC;
Jacob Pan16ab5392010-02-12 03:08:30 -0800119 mp_irq.irqtype = mp_INT;
120/* triggering mode edge bit 2-3, active high polarity bit 0-1 */
121 mp_irq.irqflag = 5;
Jacob Pan9d90e492011-04-08 11:23:00 -0700122 mp_irq.srcbus = MP_BUS_ISA;
Jacob Pan16ab5392010-02-12 03:08:30 -0800123 mp_irq.srcbusirq = pentry->irq; /* IRQ */
124 mp_irq.dstapic = MP_APIC_ALL;
125 mp_irq.dstirq = pentry->irq;
Feng Tang2d8009b2010-11-19 11:33:35 +0800126 mp_save_irq(&mp_irq);
Jacob Pan16ab5392010-02-12 03:08:30 -0800127 }
128
129 return 0;
130}
131
132struct 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
154void 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
Feng Tangcf089452010-02-12 03:37:38 -0800166/* parse all the mrtc info to a global mrtc array */
167int __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
Feng Tang1da4b1c2010-11-09 11:22:58 +0000184 pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
Feng Tangcf089452010-02-12 03:37:38 -0800185 pentry = sfi_mrtc_array;
186 for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
Feng Tang1da4b1c2010-11-09 11:22:58 +0000187 pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
Feng Tangcf089452010-02-12 03:37:38 -0800188 totallen, (u32)pentry->phys_addr, pentry->irq);
Jacob Pan9d90e492011-04-08 11:23:00 -0700189 mp_irq.type = MP_INTSRC;
Feng Tangcf089452010-02-12 03:37:38 -0800190 mp_irq.irqtype = mp_INT;
Feng Tang6f207e92010-11-11 15:50:50 +0000191 mp_irq.irqflag = 0xf; /* level trigger and active low */
Jacob Pan9d90e492011-04-08 11:23:00 -0700192 mp_irq.srcbus = MP_BUS_ISA;
Feng Tangcf089452010-02-12 03:37:38 -0800193 mp_irq.srcbusirq = pentry->irq; /* IRQ */
194 mp_irq.dstapic = MP_APIC_ALL;
195 mp_irq.dstirq = pentry->irq;
Feng Tang2d8009b2010-11-19 11:33:35 +0800196 mp_save_irq(&mp_irq);
Feng Tangcf089452010-02-12 03:37:38 -0800197 }
198 return 0;
199}
200
Jacob Pan3746c6b2010-02-12 05:01:12 -0800201static unsigned long __init mrst_calibrate_tsc(void)
202{
203 unsigned long flags, fast_calibrate;
Dirk Brandewie0a915322011-11-10 13:42:53 +0000204 if (__mrst_cpu_chip == MRST_CPU_CHIP_PENWELL) {
205 u32 lo, hi, ratio, fsb;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800206
Dirk Brandewie0a915322011-11-10 13:42:53 +0000207 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
Jacob Pan3746c6b2010-02-12 05:01:12 -0800232 if (fast_calibrate)
233 return fast_calibrate;
234
235 return 0;
236}
237
Luis R. Rodriguez8fab6af2011-05-06 15:00:09 -0700238static void __init mrst_time_init(void)
Jacob Pan3746c6b2010-02-12 05:01:12 -0800239{
Jacob Pan7f05dec2010-11-09 11:28:43 +0000240 sfi_table_parse(SFI_SIG_MTMR, NULL, NULL, sfi_parse_mtmr);
Jacob Pana875c012010-05-19 12:01:25 -0700241 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 */
Jacob Pan3746c6b2010-02-12 05:01:12 -0800256 pre_init_apic_IRQ0();
257 apbt_time_init();
258}
259
Luis R. Rodriguez8fab6af2011-05-06 15:00:09 -0700260static void __cpuinit mrst_arch_setup(void)
Jacob Pan3746c6b2010-02-12 05:01:12 -0800261{
Jacob Pana0c173b2010-05-19 12:01:24 -0700262 if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x27)
H. Peter Anvina75af582010-05-19 13:40:14 -0700263 __mrst_cpu_chip = MRST_CPU_CHIP_PENWELL;
Jacob Pana0c173b2010-05-19 12:01:24 -0700264 else if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x26)
H. Peter Anvina75af582010-05-19 13:40:14 -0700265 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
Jacob Pana0c173b2010-05-19 12:01:24 -0700266 else {
267 pr_err("Unknown Moorestown CPU (%d:%d), default to Lincroft\n",
268 boot_cpu_data.x86, boot_cpu_data.x86_model);
H. Peter Anvina75af582010-05-19 13:40:14 -0700269 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
Jacob Pana0c173b2010-05-19 12:01:24 -0700270 }
271 pr_debug("Moorestown CPU %s identified\n",
H. Peter Anvina75af582010-05-19 13:40:14 -0700272 (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT) ?
Jacob Pana0c173b2010-05-19 12:01:24 -0700273 "Lincroft" : "Penwell");
274}
Jacob Pan3746c6b2010-02-12 05:01:12 -0800275
Feng Tang6d2cce62010-07-05 23:03:19 +0800276/* MID systems don't have i8042 controller */
277static int mrst_i8042_detect(void)
278{
279 return 0;
280}
281
Jacob Pan3746c6b2010-02-12 05:01:12 -0800282/*
Jacob Pan064a59b2011-11-10 13:43:05 +0000283 * 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 */
288static unsigned char mrst_get_nmi_reason(void)
289{
290 return 0;
291}
292
293/*
Thomas Gleixner3f4110a2009-08-29 14:54:20 +0200294 * Moorestown specific x86_init function overrides and early setup
295 * calls.
296 */
297void __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;
Jacob Pan5b78b672010-02-12 02:29:11 -0800301
Jacob Pan3746c6b2010-02-12 05:01:12 -0800302 x86_init.timers.timer_init = mrst_time_init;
Jacob Pana875c012010-05-19 12:01:25 -0700303 x86_init.timers.setup_percpu_clockev = x86_init_noop;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800304
305 x86_init.irqs.pre_vector_init = x86_init_noop;
306
Jacob Pana0c173b2010-05-19 12:01:24 -0700307 x86_init.oem.arch_setup = mrst_arch_setup;
308
Jacob Pana875c012010-05-19 12:01:25 -0700309 x86_cpuinit.setup_percpu_clockev = apbt_setup_secondary_clock;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800310
311 x86_platform.calibrate_tsc = mrst_calibrate_tsc;
Feng Tang6d2cce62010-07-05 23:03:19 +0800312 x86_platform.i8042_detect = mrst_i8042_detect;
Feng Tang168202c2011-02-15 00:13:32 +0800313 x86_init.timers.wallclock_init = mrst_rtc_init;
Jacob Pan064a59b2011-11-10 13:43:05 +0000314 x86_platform.get_nmi_reason = mrst_get_nmi_reason;
315
Jacob Panaf2730f2010-02-12 10:31:47 -0800316 x86_init.pci.init = pci_mrst_init;
317 x86_init.pci.fixup_irqs = x86_init_noop;
318
Jacob Pan5b78b672010-02-12 02:29:11 -0800319 legacy_pic = &null_legacy_pic;
Jacob Panfea24e22010-05-14 14:41:20 -0700320
Alek Ducfb505a2010-11-10 16:50:08 +0000321 /* Moorestown specific power_off/restart method */
322 pm_power_off = mrst_power_off;
323 machine_ops.emergency_restart = mrst_reboot;
324
Jacob Panfea24e22010-05-14 14:41:20 -0700325 /* 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;
Jacob Pan9d90e492011-04-08 11:23:00 -0700328 set_bit(MP_BUS_ISA, mp_bus_not_pci);
Thomas Gleixner3f4110a2009-08-29 14:54:20 +0200329}
Jacob Pana875c012010-05-19 12:01:25 -0700330
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 */
335static 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);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000353
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 */
358static struct sfi_gpio_table_entry *gpio_table;
359static int gpio_num_entry;
360
361static 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
390static 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 */
409struct 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
419static 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
433static 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
Feng Tangefe3ed92011-08-26 11:25:14 +0100438 spi_info->mode = SPI_MODE_0;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000439 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
447static 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
Jekyll Lai28744b32011-11-16 18:01:20 +0000490static void *tca6416_platform_data(void *info)
491{
492 static struct pca953x_platform_data tca6416;
493 struct i2c_board_info *i2c_info = info;
494 int gpio_base, intr;
495 char base_pin_name[SFI_NAME_LEN + 1];
496 char intr_pin_name[SFI_NAME_LEN + 1];
497
498 strcpy(i2c_info->type, "tca6416");
499 strcpy(base_pin_name, "tca6416_base");
500 strcpy(intr_pin_name, "tca6416_int");
501
502 gpio_base = get_gpio_by_name(base_pin_name);
503 intr = get_gpio_by_name(intr_pin_name);
504
505 if (gpio_base == -1)
506 return NULL;
507 tca6416.gpio_base = gpio_base;
508 if (intr != -1) {
509 i2c_info->irq = intr + MRST_IRQ_OFFSET;
510 tca6416.irq_base = gpio_base + MRST_IRQ_OFFSET;
511 } else {
512 i2c_info->irq = -1;
513 tca6416.irq_base = -1;
514 }
515 return &tca6416;
516}
517
518static void *mpu3050_platform_data(void *info)
519{
520 struct i2c_board_info *i2c_info = info;
521 int intr = get_gpio_by_name("mpu3050_int");
522
523 if (intr == -1)
524 return NULL;
525
526 i2c_info->irq = intr + MRST_IRQ_OFFSET;
527 return NULL;
528}
529
Feng Tang1da4b1c2010-11-09 11:22:58 +0000530static void __init *emc1403_platform_data(void *info)
531{
532 static short intr2nd_pdata;
533 struct i2c_board_info *i2c_info = info;
534 int intr = get_gpio_by_name("thermal_int");
535 int intr2nd = get_gpio_by_name("thermal_alert");
536
537 if (intr == -1 || intr2nd == -1)
538 return NULL;
539
540 i2c_info->irq = intr + MRST_IRQ_OFFSET;
541 intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
542
543 return &intr2nd_pdata;
544}
545
546static void __init *lis331dl_platform_data(void *info)
547{
548 static short intr2nd_pdata;
549 struct i2c_board_info *i2c_info = info;
550 int intr = get_gpio_by_name("accel_int");
551 int intr2nd = get_gpio_by_name("accel_2");
552
553 if (intr == -1 || intr2nd == -1)
554 return NULL;
555
556 i2c_info->irq = intr + MRST_IRQ_OFFSET;
557 intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
558
559 return &intr2nd_pdata;
560}
561
Vinod Koul86071532010-11-10 17:40:48 +0000562static void __init *no_platform_data(void *info)
563{
564 return NULL;
565}
566
Mika Westerberg360545c2011-10-18 12:41:22 +0300567static struct resource msic_resources[] = {
568 {
569 .start = INTEL_MSIC_IRQ_PHYS_BASE,
570 .end = INTEL_MSIC_IRQ_PHYS_BASE + 64 - 1,
571 .flags = IORESOURCE_MEM,
572 },
573};
574
575static struct intel_msic_platform_data msic_pdata;
576
577static struct platform_device msic_device = {
578 .name = "intel_msic",
579 .id = -1,
580 .dev = {
581 .platform_data = &msic_pdata,
582 },
583 .num_resources = ARRAY_SIZE(msic_resources),
584 .resource = msic_resources,
585};
586
587static inline bool mrst_has_msic(void)
588{
589 return mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL;
590}
591
592static int msic_scu_status_change(struct notifier_block *nb,
593 unsigned long code, void *data)
594{
595 if (code == SCU_DOWN) {
596 platform_device_unregister(&msic_device);
597 return 0;
598 }
599
600 return platform_device_register(&msic_device);
601}
602
603static int __init msic_init(void)
604{
605 static struct notifier_block msic_scu_notifier = {
606 .notifier_call = msic_scu_status_change,
607 };
608
609 /*
610 * We need to be sure that the SCU IPC is ready before MSIC device
611 * can be registered.
612 */
613 if (mrst_has_msic())
614 intel_scu_notifier_add(&msic_scu_notifier);
615
616 return 0;
617}
618arch_initcall(msic_init);
619
620/*
621 * msic_generic_platform_data - sets generic platform data for the block
622 * @info: pointer to the SFI device table entry for this block
623 * @block: MSIC block
624 *
625 * Function sets IRQ number from the SFI table entry for given device to
626 * the MSIC platform data.
627 */
628static void *msic_generic_platform_data(void *info, enum intel_msic_block block)
629{
630 struct sfi_device_table_entry *entry = info;
631
632 BUG_ON(block < 0 || block >= INTEL_MSIC_BLOCK_LAST);
633 msic_pdata.irq[block] = entry->irq;
634
635 return no_platform_data(info);
636}
637
638static void *msic_battery_platform_data(void *info)
639{
640 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_BATTERY);
641}
642
643static void *msic_gpio_platform_data(void *info)
644{
645 static struct intel_msic_gpio_pdata pdata;
646 int gpio = get_gpio_by_name("msic_gpio_base");
647
648 if (gpio < 0)
649 return NULL;
650
651 pdata.gpio_base = gpio;
652 msic_pdata.gpio = &pdata;
653
654 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_GPIO);
655}
656
657static void *msic_audio_platform_data(void *info)
658{
659 struct platform_device *pdev;
660
661 pdev = platform_device_register_simple("sst-platform", -1, NULL, 0);
662 if (IS_ERR(pdev)) {
663 pr_err("failed to create audio platform device\n");
664 return NULL;
665 }
666
667 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_AUDIO);
668}
669
670static void *msic_power_btn_platform_data(void *info)
671{
672 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_POWER_BTN);
673}
674
675static void *msic_ocd_platform_data(void *info)
676{
677 static struct intel_msic_ocd_pdata pdata;
678 int gpio = get_gpio_by_name("ocd_gpio");
679
680 if (gpio < 0)
681 return NULL;
682
683 pdata.gpio = gpio;
684 msic_pdata.ocd = &pdata;
685
686 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_OCD);
687}
688
Feng Tang1da4b1c2010-11-09 11:22:58 +0000689static const struct devs_id __initconst device_ids[] = {
William Douglas9f80d8b2011-11-10 13:50:38 +0000690 {"bma023", SFI_DEV_TYPE_I2C, 1, &no_platform_data},
Feng Tang1da4b1c2010-11-09 11:22:58 +0000691 {"pmic_gpio", SFI_DEV_TYPE_SPI, 1, &pmic_gpio_platform_data},
Feng Tangefa22122011-11-16 18:32:34 +0000692 {"pmic_gpio", SFI_DEV_TYPE_IPC, 1, &pmic_gpio_platform_data},
Feng Tang1da4b1c2010-11-09 11:22:58 +0000693 {"spi_max3111", SFI_DEV_TYPE_SPI, 0, &max3111_platform_data},
694 {"i2c_max7315", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
695 {"i2c_max7315_2", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
Jekyll Lai28744b32011-11-16 18:01:20 +0000696 {"tca6416", SFI_DEV_TYPE_I2C, 1, &tca6416_platform_data},
Feng Tang1da4b1c2010-11-09 11:22:58 +0000697 {"emc1403", SFI_DEV_TYPE_I2C, 1, &emc1403_platform_data},
698 {"i2c_accel", SFI_DEV_TYPE_I2C, 0, &lis331dl_platform_data},
Vinod Koul86071532010-11-10 17:40:48 +0000699 {"pmic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
Jekyll Lai28744b32011-11-16 18:01:20 +0000700 {"mpu3050", SFI_DEV_TYPE_I2C, 1, &mpu3050_platform_data},
Mika Westerberg360545c2011-10-18 12:41:22 +0300701
702 /* MSIC subdevices */
703 {"msic_battery", SFI_DEV_TYPE_IPC, 1, &msic_battery_platform_data},
704 {"msic_gpio", SFI_DEV_TYPE_IPC, 1, &msic_gpio_platform_data},
705 {"msic_audio", SFI_DEV_TYPE_IPC, 1, &msic_audio_platform_data},
706 {"msic_power_btn", SFI_DEV_TYPE_IPC, 1, &msic_power_btn_platform_data},
707 {"msic_ocd", SFI_DEV_TYPE_IPC, 1, &msic_ocd_platform_data},
708
Feng Tang1da4b1c2010-11-09 11:22:58 +0000709 {},
710};
711
712#define MAX_IPCDEVS 24
713static struct platform_device *ipc_devs[MAX_IPCDEVS];
714static int ipc_next_dev;
715
716#define MAX_SCU_SPI 24
717static struct spi_board_info *spi_devs[MAX_SCU_SPI];
718static int spi_next_dev;
719
720#define MAX_SCU_I2C 24
721static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
722static int i2c_bus[MAX_SCU_I2C];
723static int i2c_next_dev;
724
725static void __init intel_scu_device_register(struct platform_device *pdev)
726{
727 if(ipc_next_dev == MAX_IPCDEVS)
728 pr_err("too many SCU IPC devices");
729 else
730 ipc_devs[ipc_next_dev++] = pdev;
731}
732
733static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
734{
735 struct spi_board_info *new_dev;
736
737 if (spi_next_dev == MAX_SCU_SPI) {
738 pr_err("too many SCU SPI devices");
739 return;
740 }
741
742 new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
743 if (!new_dev) {
744 pr_err("failed to alloc mem for delayed spi dev %s\n",
745 sdev->modalias);
746 return;
747 }
748 memcpy(new_dev, sdev, sizeof(*sdev));
749
750 spi_devs[spi_next_dev++] = new_dev;
751}
752
753static void __init intel_scu_i2c_device_register(int bus,
754 struct i2c_board_info *idev)
755{
756 struct i2c_board_info *new_dev;
757
758 if (i2c_next_dev == MAX_SCU_I2C) {
759 pr_err("too many SCU I2C devices");
760 return;
761 }
762
763 new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
764 if (!new_dev) {
765 pr_err("failed to alloc mem for delayed i2c dev %s\n",
766 idev->type);
767 return;
768 }
769 memcpy(new_dev, idev, sizeof(*idev));
770
771 i2c_bus[i2c_next_dev] = bus;
772 i2c_devs[i2c_next_dev++] = new_dev;
773}
774
Alan Cox42c25442011-09-07 16:06:51 +0300775BLOCKING_NOTIFIER_HEAD(intel_scu_notifier);
776EXPORT_SYMBOL_GPL(intel_scu_notifier);
777
Feng Tang1da4b1c2010-11-09 11:22:58 +0000778/* Called by IPC driver */
779void intel_scu_devices_create(void)
780{
781 int i;
782
783 for (i = 0; i < ipc_next_dev; i++)
784 platform_device_add(ipc_devs[i]);
785
786 for (i = 0; i < spi_next_dev; i++)
787 spi_register_board_info(spi_devs[i], 1);
788
789 for (i = 0; i < i2c_next_dev; i++) {
790 struct i2c_adapter *adapter;
791 struct i2c_client *client;
792
793 adapter = i2c_get_adapter(i2c_bus[i]);
794 if (adapter) {
795 client = i2c_new_device(adapter, i2c_devs[i]);
796 if (!client)
797 pr_err("can't create i2c device %s\n",
798 i2c_devs[i]->type);
799 } else
800 i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
801 }
Alan Cox42c25442011-09-07 16:06:51 +0300802 intel_scu_notifier_post(SCU_AVAILABLE, 0L);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000803}
804EXPORT_SYMBOL_GPL(intel_scu_devices_create);
805
806/* Called by IPC driver */
807void intel_scu_devices_destroy(void)
808{
809 int i;
810
Alan Cox42c25442011-09-07 16:06:51 +0300811 intel_scu_notifier_post(SCU_DOWN, 0L);
812
Feng Tang1da4b1c2010-11-09 11:22:58 +0000813 for (i = 0; i < ipc_next_dev; i++)
814 platform_device_del(ipc_devs[i]);
815}
816EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
817
818static void __init install_irq_resource(struct platform_device *pdev, int irq)
819{
820 /* Single threaded */
821 static struct resource __initdata res = {
822 .name = "IRQ",
823 .flags = IORESOURCE_IRQ,
824 };
825 res.start = irq;
826 platform_device_add_resources(pdev, &res, 1);
827}
828
Mika Westerberg360545c2011-10-18 12:41:22 +0300829static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *entry)
Feng Tang1da4b1c2010-11-09 11:22:58 +0000830{
831 const struct devs_id *dev = device_ids;
Mika Westerberg360545c2011-10-18 12:41:22 +0300832 struct platform_device *pdev;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000833 void *pdata = NULL;
834
835 while (dev->name[0]) {
836 if (dev->type == SFI_DEV_TYPE_IPC &&
Mika Westerberg360545c2011-10-18 12:41:22 +0300837 !strncmp(dev->name, entry->name, SFI_NAME_LEN)) {
838 pdata = dev->get_platform_data(entry);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000839 break;
840 }
841 dev++;
842 }
Mika Westerberg360545c2011-10-18 12:41:22 +0300843
844 /*
845 * On Medfield the platform device creation is handled by the MSIC
846 * MFD driver so we don't need to do it here.
847 */
848 if (mrst_has_msic())
849 return;
850
851 /* ID as IRQ is a hack that will go away */
852 pdev = platform_device_alloc(entry->name, entry->irq);
853 if (pdev == NULL) {
854 pr_err("out of memory for SFI platform device '%s'.\n",
855 entry->name);
856 return;
857 }
858 install_irq_resource(pdev, entry->irq);
859
Feng Tang1da4b1c2010-11-09 11:22:58 +0000860 pdev->dev.platform_data = pdata;
861 intel_scu_device_register(pdev);
862}
863
864static void __init sfi_handle_spi_dev(struct spi_board_info *spi_info)
865{
866 const struct devs_id *dev = device_ids;
867 void *pdata = NULL;
868
869 while (dev->name[0]) {
870 if (dev->type == SFI_DEV_TYPE_SPI &&
871 !strncmp(dev->name, spi_info->modalias, SFI_NAME_LEN)) {
872 pdata = dev->get_platform_data(spi_info);
873 break;
874 }
875 dev++;
876 }
877 spi_info->platform_data = pdata;
878 if (dev->delay)
879 intel_scu_spi_device_register(spi_info);
880 else
881 spi_register_board_info(spi_info, 1);
882}
883
884static void __init sfi_handle_i2c_dev(int bus, struct i2c_board_info *i2c_info)
885{
886 const struct devs_id *dev = device_ids;
887 void *pdata = NULL;
888
889 while (dev->name[0]) {
890 if (dev->type == SFI_DEV_TYPE_I2C &&
891 !strncmp(dev->name, i2c_info->type, SFI_NAME_LEN)) {
892 pdata = dev->get_platform_data(i2c_info);
893 break;
894 }
895 dev++;
896 }
897 i2c_info->platform_data = pdata;
898
899 if (dev->delay)
900 intel_scu_i2c_device_register(bus, i2c_info);
901 else
902 i2c_register_board_info(bus, i2c_info, 1);
903 }
904
905
906static int __init sfi_parse_devs(struct sfi_table_header *table)
907{
908 struct sfi_table_simple *sb;
909 struct sfi_device_table_entry *pentry;
910 struct spi_board_info spi_info;
911 struct i2c_board_info i2c_info;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000912 int num, i, bus;
913 int ioapic;
914 struct io_apic_irq_attr irq_attr;
915
916 sb = (struct sfi_table_simple *)table;
917 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
918 pentry = (struct sfi_device_table_entry *)sb->pentry;
919
920 for (i = 0; i < num; i++, pentry++) {
Mika Westerberg153b19a2011-10-13 12:04:20 +0300921 int irq = pentry->irq;
922
923 if (irq != (u8)0xff) { /* native RTE case */
Feng Tang1da4b1c2010-11-09 11:22:58 +0000924 /* these SPI2 devices are not exposed to system as PCI
925 * devices, but they have separate RTE entry in IOAPIC
926 * so we have to enable them one by one here
927 */
Mika Westerberg153b19a2011-10-13 12:04:20 +0300928 ioapic = mp_find_ioapic(irq);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000929 irq_attr.ioapic = ioapic;
Mika Westerberg153b19a2011-10-13 12:04:20 +0300930 irq_attr.ioapic_pin = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000931 irq_attr.trigger = 1;
932 irq_attr.polarity = 1;
Mika Westerberg153b19a2011-10-13 12:04:20 +0300933 io_apic_set_pci_routing(NULL, irq, &irq_attr);
Kirill A. Shutemova94cc4e2011-08-26 12:20:59 +0100934 } else
Mika Westerberg153b19a2011-10-13 12:04:20 +0300935 irq = 0; /* No irq */
Kirill A. Shutemova94cc4e2011-08-26 12:20:59 +0100936
Feng Tang1da4b1c2010-11-09 11:22:58 +0000937 switch (pentry->type) {
938 case SFI_DEV_TYPE_IPC:
Feng Tang1da4b1c2010-11-09 11:22:58 +0000939 pr_debug("info[%2d]: IPC bus, name = %16.16s, "
Mika Westerberg360545c2011-10-18 12:41:22 +0300940 "irq = 0x%2x\n", i, pentry->name, pentry->irq);
941 sfi_handle_ipc_dev(pentry);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000942 break;
943 case SFI_DEV_TYPE_SPI:
944 memset(&spi_info, 0, sizeof(spi_info));
945 strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
Mika Westerberg153b19a2011-10-13 12:04:20 +0300946 spi_info.irq = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000947 spi_info.bus_num = pentry->host_num;
948 spi_info.chip_select = pentry->addr;
949 spi_info.max_speed_hz = pentry->max_freq;
950 pr_debug("info[%2d]: SPI bus = %d, name = %16.16s, "
951 "irq = 0x%2x, max_freq = %d, cs = %d\n", i,
952 spi_info.bus_num,
953 spi_info.modalias,
954 spi_info.irq,
955 spi_info.max_speed_hz,
956 spi_info.chip_select);
957 sfi_handle_spi_dev(&spi_info);
958 break;
959 case SFI_DEV_TYPE_I2C:
960 memset(&i2c_info, 0, sizeof(i2c_info));
961 bus = pentry->host_num;
962 strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
Mika Westerberg153b19a2011-10-13 12:04:20 +0300963 i2c_info.irq = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000964 i2c_info.addr = pentry->addr;
965 pr_debug("info[%2d]: I2C bus = %d, name = %16.16s, "
966 "irq = 0x%2x, addr = 0x%x\n", i, bus,
967 i2c_info.type,
968 i2c_info.irq,
969 i2c_info.addr);
970 sfi_handle_i2c_dev(bus, &i2c_info);
971 break;
972 case SFI_DEV_TYPE_UART:
973 case SFI_DEV_TYPE_HSI:
974 default:
975 ;
976 }
977 }
978 return 0;
979}
980
981static int __init mrst_platform_init(void)
982{
983 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
984 sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
985 return 0;
986}
987arch_initcall(mrst_platform_init);
988
989/*
990 * we will search these buttons in SFI GPIO table (by name)
991 * and register them dynamically. Please add all possible
992 * buttons here, we will shrink them if no GPIO found.
993 */
994static struct gpio_keys_button gpio_button[] = {
995 {KEY_POWER, -1, 1, "power_btn", EV_KEY, 0, 3000},
996 {KEY_PROG1, -1, 1, "prog_btn1", EV_KEY, 0, 20},
997 {KEY_PROG2, -1, 1, "prog_btn2", EV_KEY, 0, 20},
998 {SW_LID, -1, 1, "lid_switch", EV_SW, 0, 20},
999 {KEY_VOLUMEUP, -1, 1, "vol_up", EV_KEY, 0, 20},
1000 {KEY_VOLUMEDOWN, -1, 1, "vol_down", EV_KEY, 0, 20},
1001 {KEY_CAMERA, -1, 1, "camera_full", EV_KEY, 0, 20},
1002 {KEY_CAMERA_FOCUS, -1, 1, "camera_half", EV_KEY, 0, 20},
1003 {SW_KEYPAD_SLIDE, -1, 1, "MagSw1", EV_SW, 0, 20},
1004 {SW_KEYPAD_SLIDE, -1, 1, "MagSw2", EV_SW, 0, 20},
1005};
1006
1007static struct gpio_keys_platform_data mrst_gpio_keys = {
1008 .buttons = gpio_button,
1009 .rep = 1,
1010 .nbuttons = -1, /* will fill it after search */
1011};
1012
1013static struct platform_device pb_device = {
1014 .name = "gpio-keys",
1015 .id = -1,
1016 .dev = {
1017 .platform_data = &mrst_gpio_keys,
1018 },
1019};
1020
1021/*
1022 * Shrink the non-existent buttons, register the gpio button
1023 * device if there is some
1024 */
1025static int __init pb_keys_init(void)
1026{
1027 struct gpio_keys_button *gb = gpio_button;
1028 int i, num, good = 0;
1029
1030 num = sizeof(gpio_button) / sizeof(struct gpio_keys_button);
1031 for (i = 0; i < num; i++) {
1032 gb[i].gpio = get_gpio_by_name(gb[i].desc);
1033 if (gb[i].gpio == -1)
1034 continue;
1035
1036 if (i != good)
1037 gb[good] = gb[i];
1038 good++;
1039 }
1040
1041 if (good) {
1042 mrst_gpio_keys.nbuttons = good;
1043 return platform_device_register(&pb_device);
1044 }
1045 return 0;
1046}
1047late_initcall(pb_keys_init);