blob: b7f14e5b2c661ecfbfe356a5c2529444f3139532 [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 Pan16ab5392010-02-12 03:08:30 -080079/* parse all the mtimer info to a static mtimer array */
80static int __init sfi_parse_mtmr(struct sfi_table_header *table)
81{
82 struct sfi_table_simple *sb;
83 struct sfi_timer_table_entry *pentry;
84 struct mpc_intsrc mp_irq;
85 int totallen;
86
87 sb = (struct sfi_table_simple *)table;
88 if (!sfi_mtimer_num) {
89 sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
90 struct sfi_timer_table_entry);
91 pentry = (struct sfi_timer_table_entry *) sb->pentry;
92 totallen = sfi_mtimer_num * sizeof(*pentry);
93 memcpy(sfi_mtimer_array, pentry, totallen);
94 }
95
Feng Tang1da4b1c2010-11-09 11:22:58 +000096 pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
Jacob Pan16ab5392010-02-12 03:08:30 -080097 pentry = sfi_mtimer_array;
98 for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
Feng Tang1da4b1c2010-11-09 11:22:58 +000099 pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz,"
Jacob Pan16ab5392010-02-12 03:08:30 -0800100 " irq = %d\n", totallen, (u32)pentry->phys_addr,
101 pentry->freq_hz, pentry->irq);
102 if (!pentry->irq)
103 continue;
Jacob Pan9d90e492011-04-08 11:23:00 -0700104 mp_irq.type = MP_INTSRC;
Jacob Pan16ab5392010-02-12 03:08:30 -0800105 mp_irq.irqtype = mp_INT;
106/* triggering mode edge bit 2-3, active high polarity bit 0-1 */
107 mp_irq.irqflag = 5;
Jacob Pan9d90e492011-04-08 11:23:00 -0700108 mp_irq.srcbus = MP_BUS_ISA;
Jacob Pan16ab5392010-02-12 03:08:30 -0800109 mp_irq.srcbusirq = pentry->irq; /* IRQ */
110 mp_irq.dstapic = MP_APIC_ALL;
111 mp_irq.dstirq = pentry->irq;
Feng Tang2d8009b2010-11-19 11:33:35 +0800112 mp_save_irq(&mp_irq);
Jacob Pan16ab5392010-02-12 03:08:30 -0800113 }
114
115 return 0;
116}
117
118struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
119{
120 int i;
121 if (hint < sfi_mtimer_num) {
122 if (!sfi_mtimer_usage[hint]) {
123 pr_debug("hint taken for timer %d irq %d\n",\
124 hint, sfi_mtimer_array[hint].irq);
125 sfi_mtimer_usage[hint] = 1;
126 return &sfi_mtimer_array[hint];
127 }
128 }
129 /* take the first timer available */
130 for (i = 0; i < sfi_mtimer_num;) {
131 if (!sfi_mtimer_usage[i]) {
132 sfi_mtimer_usage[i] = 1;
133 return &sfi_mtimer_array[i];
134 }
135 i++;
136 }
137 return NULL;
138}
139
140void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
141{
142 int i;
143 for (i = 0; i < sfi_mtimer_num;) {
144 if (mtmr->irq == sfi_mtimer_array[i].irq) {
145 sfi_mtimer_usage[i] = 0;
146 return;
147 }
148 i++;
149 }
150}
151
Feng Tangcf089452010-02-12 03:37:38 -0800152/* parse all the mrtc info to a global mrtc array */
153int __init sfi_parse_mrtc(struct sfi_table_header *table)
154{
155 struct sfi_table_simple *sb;
156 struct sfi_rtc_table_entry *pentry;
157 struct mpc_intsrc mp_irq;
158
159 int totallen;
160
161 sb = (struct sfi_table_simple *)table;
162 if (!sfi_mrtc_num) {
163 sfi_mrtc_num = SFI_GET_NUM_ENTRIES(sb,
164 struct sfi_rtc_table_entry);
165 pentry = (struct sfi_rtc_table_entry *)sb->pentry;
166 totallen = sfi_mrtc_num * sizeof(*pentry);
167 memcpy(sfi_mrtc_array, pentry, totallen);
168 }
169
Feng Tang1da4b1c2010-11-09 11:22:58 +0000170 pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
Feng Tangcf089452010-02-12 03:37:38 -0800171 pentry = sfi_mrtc_array;
172 for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
Feng Tang1da4b1c2010-11-09 11:22:58 +0000173 pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
Feng Tangcf089452010-02-12 03:37:38 -0800174 totallen, (u32)pentry->phys_addr, pentry->irq);
Jacob Pan9d90e492011-04-08 11:23:00 -0700175 mp_irq.type = MP_INTSRC;
Feng Tangcf089452010-02-12 03:37:38 -0800176 mp_irq.irqtype = mp_INT;
Feng Tang6f207e92010-11-11 15:50:50 +0000177 mp_irq.irqflag = 0xf; /* level trigger and active low */
Jacob Pan9d90e492011-04-08 11:23:00 -0700178 mp_irq.srcbus = MP_BUS_ISA;
Feng Tangcf089452010-02-12 03:37:38 -0800179 mp_irq.srcbusirq = pentry->irq; /* IRQ */
180 mp_irq.dstapic = MP_APIC_ALL;
181 mp_irq.dstirq = pentry->irq;
Feng Tang2d8009b2010-11-19 11:33:35 +0800182 mp_save_irq(&mp_irq);
Feng Tangcf089452010-02-12 03:37:38 -0800183 }
184 return 0;
185}
186
Jacob Pan3746c6b2010-02-12 05:01:12 -0800187static unsigned long __init mrst_calibrate_tsc(void)
188{
189 unsigned long flags, fast_calibrate;
Dirk Brandewie0a915322011-11-10 13:42:53 +0000190 if (__mrst_cpu_chip == MRST_CPU_CHIP_PENWELL) {
191 u32 lo, hi, ratio, fsb;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800192
Dirk Brandewie0a915322011-11-10 13:42:53 +0000193 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
194 pr_debug("IA32 perf status is 0x%x, 0x%0x\n", lo, hi);
195 ratio = (hi >> 8) & 0x1f;
196 pr_debug("ratio is %d\n", ratio);
197 if (!ratio) {
198 pr_err("read a zero ratio, should be incorrect!\n");
199 pr_err("force tsc ratio to 16 ...\n");
200 ratio = 16;
201 }
202 rdmsr(MSR_FSB_FREQ, lo, hi);
203 if ((lo & 0x7) == 0x7)
204 fsb = PENWELL_FSB_FREQ_83SKU;
205 else
206 fsb = PENWELL_FSB_FREQ_100SKU;
207 fast_calibrate = ratio * fsb;
208 pr_debug("read penwell tsc %lu khz\n", fast_calibrate);
209 lapic_timer_frequency = fsb * 1000 / HZ;
210 /* mark tsc clocksource as reliable */
211 set_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC_RELIABLE);
212 } else {
213 local_irq_save(flags);
214 fast_calibrate = apbt_quick_calibrate();
215 local_irq_restore(flags);
216 }
217
Jacob Pan3746c6b2010-02-12 05:01:12 -0800218 if (fast_calibrate)
219 return fast_calibrate;
220
221 return 0;
222}
223
Luis R. Rodriguez8fab6af2011-05-06 15:00:09 -0700224static void __init mrst_time_init(void)
Jacob Pan3746c6b2010-02-12 05:01:12 -0800225{
Jacob Pan7f05dec2010-11-09 11:28:43 +0000226 sfi_table_parse(SFI_SIG_MTMR, NULL, NULL, sfi_parse_mtmr);
Jacob Pana875c012010-05-19 12:01:25 -0700227 switch (mrst_timer_options) {
228 case MRST_TIMER_APBT_ONLY:
229 break;
230 case MRST_TIMER_LAPIC_APBT:
231 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
232 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
233 break;
234 default:
235 if (!boot_cpu_has(X86_FEATURE_ARAT))
236 break;
237 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
238 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
239 return;
240 }
241 /* we need at least one APB timer */
Jacob Pan3746c6b2010-02-12 05:01:12 -0800242 pre_init_apic_IRQ0();
243 apbt_time_init();
244}
245
Luis R. Rodriguez8fab6af2011-05-06 15:00:09 -0700246static void __cpuinit mrst_arch_setup(void)
Jacob Pan3746c6b2010-02-12 05:01:12 -0800247{
Jacob Pana0c173b2010-05-19 12:01:24 -0700248 if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x27)
H. Peter Anvina75af582010-05-19 13:40:14 -0700249 __mrst_cpu_chip = MRST_CPU_CHIP_PENWELL;
Jacob Pana0c173b2010-05-19 12:01:24 -0700250 else if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x26)
H. Peter Anvina75af582010-05-19 13:40:14 -0700251 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
Jacob Pana0c173b2010-05-19 12:01:24 -0700252 else {
253 pr_err("Unknown Moorestown CPU (%d:%d), default to Lincroft\n",
254 boot_cpu_data.x86, boot_cpu_data.x86_model);
H. Peter Anvina75af582010-05-19 13:40:14 -0700255 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
Jacob Pana0c173b2010-05-19 12:01:24 -0700256 }
257 pr_debug("Moorestown CPU %s identified\n",
H. Peter Anvina75af582010-05-19 13:40:14 -0700258 (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT) ?
Jacob Pana0c173b2010-05-19 12:01:24 -0700259 "Lincroft" : "Penwell");
260}
Jacob Pan3746c6b2010-02-12 05:01:12 -0800261
Feng Tang6d2cce62010-07-05 23:03:19 +0800262/* MID systems don't have i8042 controller */
263static int mrst_i8042_detect(void)
264{
265 return 0;
266}
267
Alek Ducfb505a2010-11-10 16:50:08 +0000268/* Reboot and power off are handled by the SCU on a MID device */
269static void mrst_power_off(void)
270{
271 intel_scu_ipc_simple_command(0xf1, 1);
272}
273
274static void mrst_reboot(void)
275{
276 intel_scu_ipc_simple_command(0xf1, 0);
277}
278
Jacob Pan3746c6b2010-02-12 05:01:12 -0800279/*
Thomas Gleixner3f4110a2009-08-29 14:54:20 +0200280 * Moorestown specific x86_init function overrides and early setup
281 * calls.
282 */
283void __init x86_mrst_early_setup(void)
284{
285 x86_init.resources.probe_roms = x86_init_noop;
286 x86_init.resources.reserve_resources = x86_init_noop;
Jacob Pan5b78b672010-02-12 02:29:11 -0800287
Jacob Pan3746c6b2010-02-12 05:01:12 -0800288 x86_init.timers.timer_init = mrst_time_init;
Jacob Pana875c012010-05-19 12:01:25 -0700289 x86_init.timers.setup_percpu_clockev = x86_init_noop;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800290
291 x86_init.irqs.pre_vector_init = x86_init_noop;
292
Jacob Pana0c173b2010-05-19 12:01:24 -0700293 x86_init.oem.arch_setup = mrst_arch_setup;
294
Jacob Pana875c012010-05-19 12:01:25 -0700295 x86_cpuinit.setup_percpu_clockev = apbt_setup_secondary_clock;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800296
297 x86_platform.calibrate_tsc = mrst_calibrate_tsc;
Feng Tang6d2cce62010-07-05 23:03:19 +0800298 x86_platform.i8042_detect = mrst_i8042_detect;
Feng Tang168202c2011-02-15 00:13:32 +0800299 x86_init.timers.wallclock_init = mrst_rtc_init;
Jacob Panaf2730f2010-02-12 10:31:47 -0800300 x86_init.pci.init = pci_mrst_init;
301 x86_init.pci.fixup_irqs = x86_init_noop;
302
Jacob Pan5b78b672010-02-12 02:29:11 -0800303 legacy_pic = &null_legacy_pic;
Jacob Panfea24e22010-05-14 14:41:20 -0700304
Alek Ducfb505a2010-11-10 16:50:08 +0000305 /* Moorestown specific power_off/restart method */
306 pm_power_off = mrst_power_off;
307 machine_ops.emergency_restart = mrst_reboot;
308
Jacob Panfea24e22010-05-14 14:41:20 -0700309 /* Avoid searching for BIOS MP tables */
310 x86_init.mpparse.find_smp_config = x86_init_noop;
311 x86_init.mpparse.get_smp_config = x86_init_uint_noop;
Jacob Pan9d90e492011-04-08 11:23:00 -0700312 set_bit(MP_BUS_ISA, mp_bus_not_pci);
Thomas Gleixner3f4110a2009-08-29 14:54:20 +0200313}
Jacob Pana875c012010-05-19 12:01:25 -0700314
315/*
316 * if user does not want to use per CPU apb timer, just give it a lower rating
317 * than local apic timer and skip the late per cpu timer init.
318 */
319static inline int __init setup_x86_mrst_timer(char *arg)
320{
321 if (!arg)
322 return -EINVAL;
323
324 if (strcmp("apbt_only", arg) == 0)
325 mrst_timer_options = MRST_TIMER_APBT_ONLY;
326 else if (strcmp("lapic_and_apbt", arg) == 0)
327 mrst_timer_options = MRST_TIMER_LAPIC_APBT;
328 else {
329 pr_warning("X86 MRST timer option %s not recognised"
330 " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
331 arg);
332 return -EINVAL;
333 }
334 return 0;
335}
336__setup("x86_mrst_timer=", setup_x86_mrst_timer);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000337
338/*
339 * Parsing GPIO table first, since the DEVS table will need this table
340 * to map the pin name to the actual pin.
341 */
342static struct sfi_gpio_table_entry *gpio_table;
343static int gpio_num_entry;
344
345static int __init sfi_parse_gpio(struct sfi_table_header *table)
346{
347 struct sfi_table_simple *sb;
348 struct sfi_gpio_table_entry *pentry;
349 int num, i;
350
351 if (gpio_table)
352 return 0;
353 sb = (struct sfi_table_simple *)table;
354 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
355 pentry = (struct sfi_gpio_table_entry *)sb->pentry;
356
357 gpio_table = (struct sfi_gpio_table_entry *)
358 kmalloc(num * sizeof(*pentry), GFP_KERNEL);
359 if (!gpio_table)
360 return -1;
361 memcpy(gpio_table, pentry, num * sizeof(*pentry));
362 gpio_num_entry = num;
363
364 pr_debug("GPIO pin info:\n");
365 for (i = 0; i < num; i++, pentry++)
366 pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
367 " pin = %d\n", i,
368 pentry->controller_name,
369 pentry->pin_name,
370 pentry->pin_no);
371 return 0;
372}
373
374static int get_gpio_by_name(const char *name)
375{
376 struct sfi_gpio_table_entry *pentry = gpio_table;
377 int i;
378
379 if (!pentry)
380 return -1;
381 for (i = 0; i < gpio_num_entry; i++, pentry++) {
382 if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
383 return pentry->pin_no;
384 }
385 return -1;
386}
387
388/*
389 * Here defines the array of devices platform data that IAFW would export
390 * through SFI "DEVS" table, we use name and type to match the device and
391 * its platform data.
392 */
393struct devs_id {
394 char name[SFI_NAME_LEN + 1];
395 u8 type;
396 u8 delay;
397 void *(*get_platform_data)(void *info);
398};
399
400/* the offset for the mapping of global gpio pin to irq */
401#define MRST_IRQ_OFFSET 0x100
402
403static void __init *pmic_gpio_platform_data(void *info)
404{
405 static struct intel_pmic_gpio_platform_data pmic_gpio_pdata;
406 int gpio_base = get_gpio_by_name("pmic_gpio_base");
407
408 if (gpio_base == -1)
409 gpio_base = 64;
410 pmic_gpio_pdata.gpio_base = gpio_base;
411 pmic_gpio_pdata.irq_base = gpio_base + MRST_IRQ_OFFSET;
412 pmic_gpio_pdata.gpiointr = 0xffffeff8;
413
414 return &pmic_gpio_pdata;
415}
416
417static void __init *max3111_platform_data(void *info)
418{
419 struct spi_board_info *spi_info = info;
420 int intr = get_gpio_by_name("max3111_int");
421
Feng Tangefe3ed92011-08-26 11:25:14 +0100422 spi_info->mode = SPI_MODE_0;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000423 if (intr == -1)
424 return NULL;
425 spi_info->irq = intr + MRST_IRQ_OFFSET;
426 return NULL;
427}
428
429/* we have multiple max7315 on the board ... */
430#define MAX7315_NUM 2
431static void __init *max7315_platform_data(void *info)
432{
433 static struct pca953x_platform_data max7315_pdata[MAX7315_NUM];
434 static int nr;
435 struct pca953x_platform_data *max7315 = &max7315_pdata[nr];
436 struct i2c_board_info *i2c_info = info;
437 int gpio_base, intr;
438 char base_pin_name[SFI_NAME_LEN + 1];
439 char intr_pin_name[SFI_NAME_LEN + 1];
440
441 if (nr == MAX7315_NUM) {
442 pr_err("too many max7315s, we only support %d\n",
443 MAX7315_NUM);
444 return NULL;
445 }
446 /* we have several max7315 on the board, we only need load several
447 * instances of the same pca953x driver to cover them
448 */
449 strcpy(i2c_info->type, "max7315");
450 if (nr++) {
451 sprintf(base_pin_name, "max7315_%d_base", nr);
452 sprintf(intr_pin_name, "max7315_%d_int", nr);
453 } else {
454 strcpy(base_pin_name, "max7315_base");
455 strcpy(intr_pin_name, "max7315_int");
456 }
457
458 gpio_base = get_gpio_by_name(base_pin_name);
459 intr = get_gpio_by_name(intr_pin_name);
460
461 if (gpio_base == -1)
462 return NULL;
463 max7315->gpio_base = gpio_base;
464 if (intr != -1) {
465 i2c_info->irq = intr + MRST_IRQ_OFFSET;
466 max7315->irq_base = gpio_base + MRST_IRQ_OFFSET;
467 } else {
468 i2c_info->irq = -1;
469 max7315->irq_base = -1;
470 }
471 return max7315;
472}
473
474static void __init *emc1403_platform_data(void *info)
475{
476 static short intr2nd_pdata;
477 struct i2c_board_info *i2c_info = info;
478 int intr = get_gpio_by_name("thermal_int");
479 int intr2nd = get_gpio_by_name("thermal_alert");
480
481 if (intr == -1 || intr2nd == -1)
482 return NULL;
483
484 i2c_info->irq = intr + MRST_IRQ_OFFSET;
485 intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
486
487 return &intr2nd_pdata;
488}
489
490static void __init *lis331dl_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("accel_int");
495 int intr2nd = get_gpio_by_name("accel_2");
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
Vinod Koul86071532010-11-10 17:40:48 +0000506static void __init *no_platform_data(void *info)
507{
508 return NULL;
509}
510
Mika Westerberg360545c2011-10-18 12:41:22 +0300511static struct resource msic_resources[] = {
512 {
513 .start = INTEL_MSIC_IRQ_PHYS_BASE,
514 .end = INTEL_MSIC_IRQ_PHYS_BASE + 64 - 1,
515 .flags = IORESOURCE_MEM,
516 },
517};
518
519static struct intel_msic_platform_data msic_pdata;
520
521static struct platform_device msic_device = {
522 .name = "intel_msic",
523 .id = -1,
524 .dev = {
525 .platform_data = &msic_pdata,
526 },
527 .num_resources = ARRAY_SIZE(msic_resources),
528 .resource = msic_resources,
529};
530
531static inline bool mrst_has_msic(void)
532{
533 return mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL;
534}
535
536static int msic_scu_status_change(struct notifier_block *nb,
537 unsigned long code, void *data)
538{
539 if (code == SCU_DOWN) {
540 platform_device_unregister(&msic_device);
541 return 0;
542 }
543
544 return platform_device_register(&msic_device);
545}
546
547static int __init msic_init(void)
548{
549 static struct notifier_block msic_scu_notifier = {
550 .notifier_call = msic_scu_status_change,
551 };
552
553 /*
554 * We need to be sure that the SCU IPC is ready before MSIC device
555 * can be registered.
556 */
557 if (mrst_has_msic())
558 intel_scu_notifier_add(&msic_scu_notifier);
559
560 return 0;
561}
562arch_initcall(msic_init);
563
564/*
565 * msic_generic_platform_data - sets generic platform data for the block
566 * @info: pointer to the SFI device table entry for this block
567 * @block: MSIC block
568 *
569 * Function sets IRQ number from the SFI table entry for given device to
570 * the MSIC platform data.
571 */
572static void *msic_generic_platform_data(void *info, enum intel_msic_block block)
573{
574 struct sfi_device_table_entry *entry = info;
575
576 BUG_ON(block < 0 || block >= INTEL_MSIC_BLOCK_LAST);
577 msic_pdata.irq[block] = entry->irq;
578
579 return no_platform_data(info);
580}
581
582static void *msic_battery_platform_data(void *info)
583{
584 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_BATTERY);
585}
586
587static void *msic_gpio_platform_data(void *info)
588{
589 static struct intel_msic_gpio_pdata pdata;
590 int gpio = get_gpio_by_name("msic_gpio_base");
591
592 if (gpio < 0)
593 return NULL;
594
595 pdata.gpio_base = gpio;
596 msic_pdata.gpio = &pdata;
597
598 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_GPIO);
599}
600
601static void *msic_audio_platform_data(void *info)
602{
603 struct platform_device *pdev;
604
605 pdev = platform_device_register_simple("sst-platform", -1, NULL, 0);
606 if (IS_ERR(pdev)) {
607 pr_err("failed to create audio platform device\n");
608 return NULL;
609 }
610
611 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_AUDIO);
612}
613
614static void *msic_power_btn_platform_data(void *info)
615{
616 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_POWER_BTN);
617}
618
619static void *msic_ocd_platform_data(void *info)
620{
621 static struct intel_msic_ocd_pdata pdata;
622 int gpio = get_gpio_by_name("ocd_gpio");
623
624 if (gpio < 0)
625 return NULL;
626
627 pdata.gpio = gpio;
628 msic_pdata.ocd = &pdata;
629
630 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_OCD);
631}
632
Feng Tang1da4b1c2010-11-09 11:22:58 +0000633static const struct devs_id __initconst device_ids[] = {
634 {"pmic_gpio", SFI_DEV_TYPE_SPI, 1, &pmic_gpio_platform_data},
635 {"spi_max3111", SFI_DEV_TYPE_SPI, 0, &max3111_platform_data},
636 {"i2c_max7315", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
637 {"i2c_max7315_2", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
638 {"emc1403", SFI_DEV_TYPE_I2C, 1, &emc1403_platform_data},
639 {"i2c_accel", SFI_DEV_TYPE_I2C, 0, &lis331dl_platform_data},
Vinod Koul86071532010-11-10 17:40:48 +0000640 {"pmic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
Mika Westerberg360545c2011-10-18 12:41:22 +0300641
642 /* MSIC subdevices */
643 {"msic_battery", SFI_DEV_TYPE_IPC, 1, &msic_battery_platform_data},
644 {"msic_gpio", SFI_DEV_TYPE_IPC, 1, &msic_gpio_platform_data},
645 {"msic_audio", SFI_DEV_TYPE_IPC, 1, &msic_audio_platform_data},
646 {"msic_power_btn", SFI_DEV_TYPE_IPC, 1, &msic_power_btn_platform_data},
647 {"msic_ocd", SFI_DEV_TYPE_IPC, 1, &msic_ocd_platform_data},
648
Feng Tang1da4b1c2010-11-09 11:22:58 +0000649 {},
650};
651
652#define MAX_IPCDEVS 24
653static struct platform_device *ipc_devs[MAX_IPCDEVS];
654static int ipc_next_dev;
655
656#define MAX_SCU_SPI 24
657static struct spi_board_info *spi_devs[MAX_SCU_SPI];
658static int spi_next_dev;
659
660#define MAX_SCU_I2C 24
661static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
662static int i2c_bus[MAX_SCU_I2C];
663static int i2c_next_dev;
664
665static void __init intel_scu_device_register(struct platform_device *pdev)
666{
667 if(ipc_next_dev == MAX_IPCDEVS)
668 pr_err("too many SCU IPC devices");
669 else
670 ipc_devs[ipc_next_dev++] = pdev;
671}
672
673static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
674{
675 struct spi_board_info *new_dev;
676
677 if (spi_next_dev == MAX_SCU_SPI) {
678 pr_err("too many SCU SPI devices");
679 return;
680 }
681
682 new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
683 if (!new_dev) {
684 pr_err("failed to alloc mem for delayed spi dev %s\n",
685 sdev->modalias);
686 return;
687 }
688 memcpy(new_dev, sdev, sizeof(*sdev));
689
690 spi_devs[spi_next_dev++] = new_dev;
691}
692
693static void __init intel_scu_i2c_device_register(int bus,
694 struct i2c_board_info *idev)
695{
696 struct i2c_board_info *new_dev;
697
698 if (i2c_next_dev == MAX_SCU_I2C) {
699 pr_err("too many SCU I2C devices");
700 return;
701 }
702
703 new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
704 if (!new_dev) {
705 pr_err("failed to alloc mem for delayed i2c dev %s\n",
706 idev->type);
707 return;
708 }
709 memcpy(new_dev, idev, sizeof(*idev));
710
711 i2c_bus[i2c_next_dev] = bus;
712 i2c_devs[i2c_next_dev++] = new_dev;
713}
714
Alan Cox42c25442011-09-07 16:06:51 +0300715BLOCKING_NOTIFIER_HEAD(intel_scu_notifier);
716EXPORT_SYMBOL_GPL(intel_scu_notifier);
717
Feng Tang1da4b1c2010-11-09 11:22:58 +0000718/* Called by IPC driver */
719void intel_scu_devices_create(void)
720{
721 int i;
722
723 for (i = 0; i < ipc_next_dev; i++)
724 platform_device_add(ipc_devs[i]);
725
726 for (i = 0; i < spi_next_dev; i++)
727 spi_register_board_info(spi_devs[i], 1);
728
729 for (i = 0; i < i2c_next_dev; i++) {
730 struct i2c_adapter *adapter;
731 struct i2c_client *client;
732
733 adapter = i2c_get_adapter(i2c_bus[i]);
734 if (adapter) {
735 client = i2c_new_device(adapter, i2c_devs[i]);
736 if (!client)
737 pr_err("can't create i2c device %s\n",
738 i2c_devs[i]->type);
739 } else
740 i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
741 }
Alan Cox42c25442011-09-07 16:06:51 +0300742 intel_scu_notifier_post(SCU_AVAILABLE, 0L);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000743}
744EXPORT_SYMBOL_GPL(intel_scu_devices_create);
745
746/* Called by IPC driver */
747void intel_scu_devices_destroy(void)
748{
749 int i;
750
Alan Cox42c25442011-09-07 16:06:51 +0300751 intel_scu_notifier_post(SCU_DOWN, 0L);
752
Feng Tang1da4b1c2010-11-09 11:22:58 +0000753 for (i = 0; i < ipc_next_dev; i++)
754 platform_device_del(ipc_devs[i]);
755}
756EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
757
758static void __init install_irq_resource(struct platform_device *pdev, int irq)
759{
760 /* Single threaded */
761 static struct resource __initdata res = {
762 .name = "IRQ",
763 .flags = IORESOURCE_IRQ,
764 };
765 res.start = irq;
766 platform_device_add_resources(pdev, &res, 1);
767}
768
Mika Westerberg360545c2011-10-18 12:41:22 +0300769static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *entry)
Feng Tang1da4b1c2010-11-09 11:22:58 +0000770{
771 const struct devs_id *dev = device_ids;
Mika Westerberg360545c2011-10-18 12:41:22 +0300772 struct platform_device *pdev;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000773 void *pdata = NULL;
774
775 while (dev->name[0]) {
776 if (dev->type == SFI_DEV_TYPE_IPC &&
Mika Westerberg360545c2011-10-18 12:41:22 +0300777 !strncmp(dev->name, entry->name, SFI_NAME_LEN)) {
778 pdata = dev->get_platform_data(entry);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000779 break;
780 }
781 dev++;
782 }
Mika Westerberg360545c2011-10-18 12:41:22 +0300783
784 /*
785 * On Medfield the platform device creation is handled by the MSIC
786 * MFD driver so we don't need to do it here.
787 */
788 if (mrst_has_msic())
789 return;
790
791 /* ID as IRQ is a hack that will go away */
792 pdev = platform_device_alloc(entry->name, entry->irq);
793 if (pdev == NULL) {
794 pr_err("out of memory for SFI platform device '%s'.\n",
795 entry->name);
796 return;
797 }
798 install_irq_resource(pdev, entry->irq);
799
Feng Tang1da4b1c2010-11-09 11:22:58 +0000800 pdev->dev.platform_data = pdata;
801 intel_scu_device_register(pdev);
802}
803
804static void __init sfi_handle_spi_dev(struct spi_board_info *spi_info)
805{
806 const struct devs_id *dev = device_ids;
807 void *pdata = NULL;
808
809 while (dev->name[0]) {
810 if (dev->type == SFI_DEV_TYPE_SPI &&
811 !strncmp(dev->name, spi_info->modalias, SFI_NAME_LEN)) {
812 pdata = dev->get_platform_data(spi_info);
813 break;
814 }
815 dev++;
816 }
817 spi_info->platform_data = pdata;
818 if (dev->delay)
819 intel_scu_spi_device_register(spi_info);
820 else
821 spi_register_board_info(spi_info, 1);
822}
823
824static void __init sfi_handle_i2c_dev(int bus, struct i2c_board_info *i2c_info)
825{
826 const struct devs_id *dev = device_ids;
827 void *pdata = NULL;
828
829 while (dev->name[0]) {
830 if (dev->type == SFI_DEV_TYPE_I2C &&
831 !strncmp(dev->name, i2c_info->type, SFI_NAME_LEN)) {
832 pdata = dev->get_platform_data(i2c_info);
833 break;
834 }
835 dev++;
836 }
837 i2c_info->platform_data = pdata;
838
839 if (dev->delay)
840 intel_scu_i2c_device_register(bus, i2c_info);
841 else
842 i2c_register_board_info(bus, i2c_info, 1);
843 }
844
845
846static int __init sfi_parse_devs(struct sfi_table_header *table)
847{
848 struct sfi_table_simple *sb;
849 struct sfi_device_table_entry *pentry;
850 struct spi_board_info spi_info;
851 struct i2c_board_info i2c_info;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000852 int num, i, bus;
853 int ioapic;
854 struct io_apic_irq_attr irq_attr;
855
856 sb = (struct sfi_table_simple *)table;
857 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
858 pentry = (struct sfi_device_table_entry *)sb->pentry;
859
860 for (i = 0; i < num; i++, pentry++) {
Mika Westerberg153b19a2011-10-13 12:04:20 +0300861 int irq = pentry->irq;
862
863 if (irq != (u8)0xff) { /* native RTE case */
Feng Tang1da4b1c2010-11-09 11:22:58 +0000864 /* these SPI2 devices are not exposed to system as PCI
865 * devices, but they have separate RTE entry in IOAPIC
866 * so we have to enable them one by one here
867 */
Mika Westerberg153b19a2011-10-13 12:04:20 +0300868 ioapic = mp_find_ioapic(irq);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000869 irq_attr.ioapic = ioapic;
Mika Westerberg153b19a2011-10-13 12:04:20 +0300870 irq_attr.ioapic_pin = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000871 irq_attr.trigger = 1;
872 irq_attr.polarity = 1;
Mika Westerberg153b19a2011-10-13 12:04:20 +0300873 io_apic_set_pci_routing(NULL, irq, &irq_attr);
Kirill A. Shutemova94cc4e2011-08-26 12:20:59 +0100874 } else
Mika Westerberg153b19a2011-10-13 12:04:20 +0300875 irq = 0; /* No irq */
Kirill A. Shutemova94cc4e2011-08-26 12:20:59 +0100876
Feng Tang1da4b1c2010-11-09 11:22:58 +0000877 switch (pentry->type) {
878 case SFI_DEV_TYPE_IPC:
Feng Tang1da4b1c2010-11-09 11:22:58 +0000879 pr_debug("info[%2d]: IPC bus, name = %16.16s, "
Mika Westerberg360545c2011-10-18 12:41:22 +0300880 "irq = 0x%2x\n", i, pentry->name, pentry->irq);
881 sfi_handle_ipc_dev(pentry);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000882 break;
883 case SFI_DEV_TYPE_SPI:
884 memset(&spi_info, 0, sizeof(spi_info));
885 strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
Mika Westerberg153b19a2011-10-13 12:04:20 +0300886 spi_info.irq = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000887 spi_info.bus_num = pentry->host_num;
888 spi_info.chip_select = pentry->addr;
889 spi_info.max_speed_hz = pentry->max_freq;
890 pr_debug("info[%2d]: SPI bus = %d, name = %16.16s, "
891 "irq = 0x%2x, max_freq = %d, cs = %d\n", i,
892 spi_info.bus_num,
893 spi_info.modalias,
894 spi_info.irq,
895 spi_info.max_speed_hz,
896 spi_info.chip_select);
897 sfi_handle_spi_dev(&spi_info);
898 break;
899 case SFI_DEV_TYPE_I2C:
900 memset(&i2c_info, 0, sizeof(i2c_info));
901 bus = pentry->host_num;
902 strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
Mika Westerberg153b19a2011-10-13 12:04:20 +0300903 i2c_info.irq = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000904 i2c_info.addr = pentry->addr;
905 pr_debug("info[%2d]: I2C bus = %d, name = %16.16s, "
906 "irq = 0x%2x, addr = 0x%x\n", i, bus,
907 i2c_info.type,
908 i2c_info.irq,
909 i2c_info.addr);
910 sfi_handle_i2c_dev(bus, &i2c_info);
911 break;
912 case SFI_DEV_TYPE_UART:
913 case SFI_DEV_TYPE_HSI:
914 default:
915 ;
916 }
917 }
918 return 0;
919}
920
921static int __init mrst_platform_init(void)
922{
923 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
924 sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
925 return 0;
926}
927arch_initcall(mrst_platform_init);
928
929/*
930 * we will search these buttons in SFI GPIO table (by name)
931 * and register them dynamically. Please add all possible
932 * buttons here, we will shrink them if no GPIO found.
933 */
934static struct gpio_keys_button gpio_button[] = {
935 {KEY_POWER, -1, 1, "power_btn", EV_KEY, 0, 3000},
936 {KEY_PROG1, -1, 1, "prog_btn1", EV_KEY, 0, 20},
937 {KEY_PROG2, -1, 1, "prog_btn2", EV_KEY, 0, 20},
938 {SW_LID, -1, 1, "lid_switch", EV_SW, 0, 20},
939 {KEY_VOLUMEUP, -1, 1, "vol_up", EV_KEY, 0, 20},
940 {KEY_VOLUMEDOWN, -1, 1, "vol_down", EV_KEY, 0, 20},
941 {KEY_CAMERA, -1, 1, "camera_full", EV_KEY, 0, 20},
942 {KEY_CAMERA_FOCUS, -1, 1, "camera_half", EV_KEY, 0, 20},
943 {SW_KEYPAD_SLIDE, -1, 1, "MagSw1", EV_SW, 0, 20},
944 {SW_KEYPAD_SLIDE, -1, 1, "MagSw2", EV_SW, 0, 20},
945};
946
947static struct gpio_keys_platform_data mrst_gpio_keys = {
948 .buttons = gpio_button,
949 .rep = 1,
950 .nbuttons = -1, /* will fill it after search */
951};
952
953static struct platform_device pb_device = {
954 .name = "gpio-keys",
955 .id = -1,
956 .dev = {
957 .platform_data = &mrst_gpio_keys,
958 },
959};
960
961/*
962 * Shrink the non-existent buttons, register the gpio button
963 * device if there is some
964 */
965static int __init pb_keys_init(void)
966{
967 struct gpio_keys_button *gb = gpio_button;
968 int i, num, good = 0;
969
970 num = sizeof(gpio_button) / sizeof(struct gpio_keys_button);
971 for (i = 0; i < num; i++) {
972 gb[i].gpio = get_gpio_by_name(gb[i].desc);
973 if (gb[i].gpio == -1)
974 continue;
975
976 if (i != good)
977 gb[good] = gb[i];
978 good++;
979 }
980
981 if (good) {
982 mrst_gpio_keys.nbuttons = good;
983 return platform_device_register(&pb_device);
984 }
985 return 0;
986}
987late_initcall(pb_keys_init);