blob: 541020df0da6713f485b313829def31dab03b334 [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;
190
191 local_irq_save(flags);
192 fast_calibrate = apbt_quick_calibrate();
193 local_irq_restore(flags);
194
195 if (fast_calibrate)
196 return fast_calibrate;
197
198 return 0;
199}
200
Luis R. Rodriguez8fab6af2011-05-06 15:00:09 -0700201static void __init mrst_time_init(void)
Jacob Pan3746c6b2010-02-12 05:01:12 -0800202{
Jacob Pan7f05dec2010-11-09 11:28:43 +0000203 sfi_table_parse(SFI_SIG_MTMR, NULL, NULL, sfi_parse_mtmr);
Jacob Pana875c012010-05-19 12:01:25 -0700204 switch (mrst_timer_options) {
205 case MRST_TIMER_APBT_ONLY:
206 break;
207 case MRST_TIMER_LAPIC_APBT:
208 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
209 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
210 break;
211 default:
212 if (!boot_cpu_has(X86_FEATURE_ARAT))
213 break;
214 x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
215 x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
216 return;
217 }
218 /* we need at least one APB timer */
Jacob Pan3746c6b2010-02-12 05:01:12 -0800219 pre_init_apic_IRQ0();
220 apbt_time_init();
221}
222
Luis R. Rodriguez8fab6af2011-05-06 15:00:09 -0700223static void __cpuinit mrst_arch_setup(void)
Jacob Pan3746c6b2010-02-12 05:01:12 -0800224{
Jacob Pana0c173b2010-05-19 12:01:24 -0700225 if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x27)
H. Peter Anvina75af582010-05-19 13:40:14 -0700226 __mrst_cpu_chip = MRST_CPU_CHIP_PENWELL;
Jacob Pana0c173b2010-05-19 12:01:24 -0700227 else if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x26)
H. Peter Anvina75af582010-05-19 13:40:14 -0700228 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
Jacob Pana0c173b2010-05-19 12:01:24 -0700229 else {
230 pr_err("Unknown Moorestown CPU (%d:%d), default to Lincroft\n",
231 boot_cpu_data.x86, boot_cpu_data.x86_model);
H. Peter Anvina75af582010-05-19 13:40:14 -0700232 __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
Jacob Pana0c173b2010-05-19 12:01:24 -0700233 }
234 pr_debug("Moorestown CPU %s identified\n",
H. Peter Anvina75af582010-05-19 13:40:14 -0700235 (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT) ?
Jacob Pana0c173b2010-05-19 12:01:24 -0700236 "Lincroft" : "Penwell");
237}
Jacob Pan3746c6b2010-02-12 05:01:12 -0800238
Feng Tang6d2cce62010-07-05 23:03:19 +0800239/* MID systems don't have i8042 controller */
240static int mrst_i8042_detect(void)
241{
242 return 0;
243}
244
Alek Ducfb505a2010-11-10 16:50:08 +0000245/* Reboot and power off are handled by the SCU on a MID device */
246static void mrst_power_off(void)
247{
248 intel_scu_ipc_simple_command(0xf1, 1);
249}
250
251static void mrst_reboot(void)
252{
253 intel_scu_ipc_simple_command(0xf1, 0);
254}
255
Jacob Pan3746c6b2010-02-12 05:01:12 -0800256/*
Thomas Gleixner3f4110a2009-08-29 14:54:20 +0200257 * Moorestown specific x86_init function overrides and early setup
258 * calls.
259 */
260void __init x86_mrst_early_setup(void)
261{
262 x86_init.resources.probe_roms = x86_init_noop;
263 x86_init.resources.reserve_resources = x86_init_noop;
Jacob Pan5b78b672010-02-12 02:29:11 -0800264
Jacob Pan3746c6b2010-02-12 05:01:12 -0800265 x86_init.timers.timer_init = mrst_time_init;
Jacob Pana875c012010-05-19 12:01:25 -0700266 x86_init.timers.setup_percpu_clockev = x86_init_noop;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800267
268 x86_init.irqs.pre_vector_init = x86_init_noop;
269
Jacob Pana0c173b2010-05-19 12:01:24 -0700270 x86_init.oem.arch_setup = mrst_arch_setup;
271
Jacob Pana875c012010-05-19 12:01:25 -0700272 x86_cpuinit.setup_percpu_clockev = apbt_setup_secondary_clock;
Jacob Pan3746c6b2010-02-12 05:01:12 -0800273
274 x86_platform.calibrate_tsc = mrst_calibrate_tsc;
Feng Tang6d2cce62010-07-05 23:03:19 +0800275 x86_platform.i8042_detect = mrst_i8042_detect;
Feng Tang168202c2011-02-15 00:13:32 +0800276 x86_init.timers.wallclock_init = mrst_rtc_init;
Jacob Panaf2730f2010-02-12 10:31:47 -0800277 x86_init.pci.init = pci_mrst_init;
278 x86_init.pci.fixup_irqs = x86_init_noop;
279
Jacob Pan5b78b672010-02-12 02:29:11 -0800280 legacy_pic = &null_legacy_pic;
Jacob Panfea24e22010-05-14 14:41:20 -0700281
Alek Ducfb505a2010-11-10 16:50:08 +0000282 /* Moorestown specific power_off/restart method */
283 pm_power_off = mrst_power_off;
284 machine_ops.emergency_restart = mrst_reboot;
285
Jacob Panfea24e22010-05-14 14:41:20 -0700286 /* Avoid searching for BIOS MP tables */
287 x86_init.mpparse.find_smp_config = x86_init_noop;
288 x86_init.mpparse.get_smp_config = x86_init_uint_noop;
Jacob Pan9d90e492011-04-08 11:23:00 -0700289 set_bit(MP_BUS_ISA, mp_bus_not_pci);
Thomas Gleixner3f4110a2009-08-29 14:54:20 +0200290}
Jacob Pana875c012010-05-19 12:01:25 -0700291
292/*
293 * if user does not want to use per CPU apb timer, just give it a lower rating
294 * than local apic timer and skip the late per cpu timer init.
295 */
296static inline int __init setup_x86_mrst_timer(char *arg)
297{
298 if (!arg)
299 return -EINVAL;
300
301 if (strcmp("apbt_only", arg) == 0)
302 mrst_timer_options = MRST_TIMER_APBT_ONLY;
303 else if (strcmp("lapic_and_apbt", arg) == 0)
304 mrst_timer_options = MRST_TIMER_LAPIC_APBT;
305 else {
306 pr_warning("X86 MRST timer option %s not recognised"
307 " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
308 arg);
309 return -EINVAL;
310 }
311 return 0;
312}
313__setup("x86_mrst_timer=", setup_x86_mrst_timer);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000314
315/*
316 * Parsing GPIO table first, since the DEVS table will need this table
317 * to map the pin name to the actual pin.
318 */
319static struct sfi_gpio_table_entry *gpio_table;
320static int gpio_num_entry;
321
322static int __init sfi_parse_gpio(struct sfi_table_header *table)
323{
324 struct sfi_table_simple *sb;
325 struct sfi_gpio_table_entry *pentry;
326 int num, i;
327
328 if (gpio_table)
329 return 0;
330 sb = (struct sfi_table_simple *)table;
331 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
332 pentry = (struct sfi_gpio_table_entry *)sb->pentry;
333
334 gpio_table = (struct sfi_gpio_table_entry *)
335 kmalloc(num * sizeof(*pentry), GFP_KERNEL);
336 if (!gpio_table)
337 return -1;
338 memcpy(gpio_table, pentry, num * sizeof(*pentry));
339 gpio_num_entry = num;
340
341 pr_debug("GPIO pin info:\n");
342 for (i = 0; i < num; i++, pentry++)
343 pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
344 " pin = %d\n", i,
345 pentry->controller_name,
346 pentry->pin_name,
347 pentry->pin_no);
348 return 0;
349}
350
351static int get_gpio_by_name(const char *name)
352{
353 struct sfi_gpio_table_entry *pentry = gpio_table;
354 int i;
355
356 if (!pentry)
357 return -1;
358 for (i = 0; i < gpio_num_entry; i++, pentry++) {
359 if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
360 return pentry->pin_no;
361 }
362 return -1;
363}
364
365/*
366 * Here defines the array of devices platform data that IAFW would export
367 * through SFI "DEVS" table, we use name and type to match the device and
368 * its platform data.
369 */
370struct devs_id {
371 char name[SFI_NAME_LEN + 1];
372 u8 type;
373 u8 delay;
374 void *(*get_platform_data)(void *info);
375};
376
377/* the offset for the mapping of global gpio pin to irq */
378#define MRST_IRQ_OFFSET 0x100
379
380static void __init *pmic_gpio_platform_data(void *info)
381{
382 static struct intel_pmic_gpio_platform_data pmic_gpio_pdata;
383 int gpio_base = get_gpio_by_name("pmic_gpio_base");
384
385 if (gpio_base == -1)
386 gpio_base = 64;
387 pmic_gpio_pdata.gpio_base = gpio_base;
388 pmic_gpio_pdata.irq_base = gpio_base + MRST_IRQ_OFFSET;
389 pmic_gpio_pdata.gpiointr = 0xffffeff8;
390
391 return &pmic_gpio_pdata;
392}
393
394static void __init *max3111_platform_data(void *info)
395{
396 struct spi_board_info *spi_info = info;
397 int intr = get_gpio_by_name("max3111_int");
398
Feng Tangefe3ed92011-08-26 11:25:14 +0100399 spi_info->mode = SPI_MODE_0;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000400 if (intr == -1)
401 return NULL;
402 spi_info->irq = intr + MRST_IRQ_OFFSET;
403 return NULL;
404}
405
406/* we have multiple max7315 on the board ... */
407#define MAX7315_NUM 2
408static void __init *max7315_platform_data(void *info)
409{
410 static struct pca953x_platform_data max7315_pdata[MAX7315_NUM];
411 static int nr;
412 struct pca953x_platform_data *max7315 = &max7315_pdata[nr];
413 struct i2c_board_info *i2c_info = info;
414 int gpio_base, intr;
415 char base_pin_name[SFI_NAME_LEN + 1];
416 char intr_pin_name[SFI_NAME_LEN + 1];
417
418 if (nr == MAX7315_NUM) {
419 pr_err("too many max7315s, we only support %d\n",
420 MAX7315_NUM);
421 return NULL;
422 }
423 /* we have several max7315 on the board, we only need load several
424 * instances of the same pca953x driver to cover them
425 */
426 strcpy(i2c_info->type, "max7315");
427 if (nr++) {
428 sprintf(base_pin_name, "max7315_%d_base", nr);
429 sprintf(intr_pin_name, "max7315_%d_int", nr);
430 } else {
431 strcpy(base_pin_name, "max7315_base");
432 strcpy(intr_pin_name, "max7315_int");
433 }
434
435 gpio_base = get_gpio_by_name(base_pin_name);
436 intr = get_gpio_by_name(intr_pin_name);
437
438 if (gpio_base == -1)
439 return NULL;
440 max7315->gpio_base = gpio_base;
441 if (intr != -1) {
442 i2c_info->irq = intr + MRST_IRQ_OFFSET;
443 max7315->irq_base = gpio_base + MRST_IRQ_OFFSET;
444 } else {
445 i2c_info->irq = -1;
446 max7315->irq_base = -1;
447 }
448 return max7315;
449}
450
451static void __init *emc1403_platform_data(void *info)
452{
453 static short intr2nd_pdata;
454 struct i2c_board_info *i2c_info = info;
455 int intr = get_gpio_by_name("thermal_int");
456 int intr2nd = get_gpio_by_name("thermal_alert");
457
458 if (intr == -1 || intr2nd == -1)
459 return NULL;
460
461 i2c_info->irq = intr + MRST_IRQ_OFFSET;
462 intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
463
464 return &intr2nd_pdata;
465}
466
467static void __init *lis331dl_platform_data(void *info)
468{
469 static short intr2nd_pdata;
470 struct i2c_board_info *i2c_info = info;
471 int intr = get_gpio_by_name("accel_int");
472 int intr2nd = get_gpio_by_name("accel_2");
473
474 if (intr == -1 || intr2nd == -1)
475 return NULL;
476
477 i2c_info->irq = intr + MRST_IRQ_OFFSET;
478 intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
479
480 return &intr2nd_pdata;
481}
482
Vinod Koul86071532010-11-10 17:40:48 +0000483static void __init *no_platform_data(void *info)
484{
485 return NULL;
486}
487
Mika Westerberg360545c2011-10-18 12:41:22 +0300488static struct resource msic_resources[] = {
489 {
490 .start = INTEL_MSIC_IRQ_PHYS_BASE,
491 .end = INTEL_MSIC_IRQ_PHYS_BASE + 64 - 1,
492 .flags = IORESOURCE_MEM,
493 },
494};
495
496static struct intel_msic_platform_data msic_pdata;
497
498static struct platform_device msic_device = {
499 .name = "intel_msic",
500 .id = -1,
501 .dev = {
502 .platform_data = &msic_pdata,
503 },
504 .num_resources = ARRAY_SIZE(msic_resources),
505 .resource = msic_resources,
506};
507
508static inline bool mrst_has_msic(void)
509{
510 return mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL;
511}
512
513static int msic_scu_status_change(struct notifier_block *nb,
514 unsigned long code, void *data)
515{
516 if (code == SCU_DOWN) {
517 platform_device_unregister(&msic_device);
518 return 0;
519 }
520
521 return platform_device_register(&msic_device);
522}
523
524static int __init msic_init(void)
525{
526 static struct notifier_block msic_scu_notifier = {
527 .notifier_call = msic_scu_status_change,
528 };
529
530 /*
531 * We need to be sure that the SCU IPC is ready before MSIC device
532 * can be registered.
533 */
534 if (mrst_has_msic())
535 intel_scu_notifier_add(&msic_scu_notifier);
536
537 return 0;
538}
539arch_initcall(msic_init);
540
541/*
542 * msic_generic_platform_data - sets generic platform data for the block
543 * @info: pointer to the SFI device table entry for this block
544 * @block: MSIC block
545 *
546 * Function sets IRQ number from the SFI table entry for given device to
547 * the MSIC platform data.
548 */
549static void *msic_generic_platform_data(void *info, enum intel_msic_block block)
550{
551 struct sfi_device_table_entry *entry = info;
552
553 BUG_ON(block < 0 || block >= INTEL_MSIC_BLOCK_LAST);
554 msic_pdata.irq[block] = entry->irq;
555
556 return no_platform_data(info);
557}
558
559static void *msic_battery_platform_data(void *info)
560{
561 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_BATTERY);
562}
563
564static void *msic_gpio_platform_data(void *info)
565{
566 static struct intel_msic_gpio_pdata pdata;
567 int gpio = get_gpio_by_name("msic_gpio_base");
568
569 if (gpio < 0)
570 return NULL;
571
572 pdata.gpio_base = gpio;
573 msic_pdata.gpio = &pdata;
574
575 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_GPIO);
576}
577
578static void *msic_audio_platform_data(void *info)
579{
580 struct platform_device *pdev;
581
582 pdev = platform_device_register_simple("sst-platform", -1, NULL, 0);
583 if (IS_ERR(pdev)) {
584 pr_err("failed to create audio platform device\n");
585 return NULL;
586 }
587
588 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_AUDIO);
589}
590
591static void *msic_power_btn_platform_data(void *info)
592{
593 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_POWER_BTN);
594}
595
596static void *msic_ocd_platform_data(void *info)
597{
598 static struct intel_msic_ocd_pdata pdata;
599 int gpio = get_gpio_by_name("ocd_gpio");
600
601 if (gpio < 0)
602 return NULL;
603
604 pdata.gpio = gpio;
605 msic_pdata.ocd = &pdata;
606
607 return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_OCD);
608}
609
Feng Tang1da4b1c2010-11-09 11:22:58 +0000610static const struct devs_id __initconst device_ids[] = {
William Douglas9f80d8b2011-11-10 13:50:38 +0000611 {"bma023", SFI_DEV_TYPE_I2C, 1, &no_platform_data},
Feng Tang1da4b1c2010-11-09 11:22:58 +0000612 {"pmic_gpio", SFI_DEV_TYPE_SPI, 1, &pmic_gpio_platform_data},
613 {"spi_max3111", SFI_DEV_TYPE_SPI, 0, &max3111_platform_data},
614 {"i2c_max7315", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
615 {"i2c_max7315_2", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
616 {"emc1403", SFI_DEV_TYPE_I2C, 1, &emc1403_platform_data},
617 {"i2c_accel", SFI_DEV_TYPE_I2C, 0, &lis331dl_platform_data},
Vinod Koul86071532010-11-10 17:40:48 +0000618 {"pmic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
Mika Westerberg360545c2011-10-18 12:41:22 +0300619
620 /* MSIC subdevices */
621 {"msic_battery", SFI_DEV_TYPE_IPC, 1, &msic_battery_platform_data},
622 {"msic_gpio", SFI_DEV_TYPE_IPC, 1, &msic_gpio_platform_data},
623 {"msic_audio", SFI_DEV_TYPE_IPC, 1, &msic_audio_platform_data},
624 {"msic_power_btn", SFI_DEV_TYPE_IPC, 1, &msic_power_btn_platform_data},
625 {"msic_ocd", SFI_DEV_TYPE_IPC, 1, &msic_ocd_platform_data},
626
Feng Tang1da4b1c2010-11-09 11:22:58 +0000627 {},
628};
629
630#define MAX_IPCDEVS 24
631static struct platform_device *ipc_devs[MAX_IPCDEVS];
632static int ipc_next_dev;
633
634#define MAX_SCU_SPI 24
635static struct spi_board_info *spi_devs[MAX_SCU_SPI];
636static int spi_next_dev;
637
638#define MAX_SCU_I2C 24
639static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
640static int i2c_bus[MAX_SCU_I2C];
641static int i2c_next_dev;
642
643static void __init intel_scu_device_register(struct platform_device *pdev)
644{
645 if(ipc_next_dev == MAX_IPCDEVS)
646 pr_err("too many SCU IPC devices");
647 else
648 ipc_devs[ipc_next_dev++] = pdev;
649}
650
651static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
652{
653 struct spi_board_info *new_dev;
654
655 if (spi_next_dev == MAX_SCU_SPI) {
656 pr_err("too many SCU SPI devices");
657 return;
658 }
659
660 new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
661 if (!new_dev) {
662 pr_err("failed to alloc mem for delayed spi dev %s\n",
663 sdev->modalias);
664 return;
665 }
666 memcpy(new_dev, sdev, sizeof(*sdev));
667
668 spi_devs[spi_next_dev++] = new_dev;
669}
670
671static void __init intel_scu_i2c_device_register(int bus,
672 struct i2c_board_info *idev)
673{
674 struct i2c_board_info *new_dev;
675
676 if (i2c_next_dev == MAX_SCU_I2C) {
677 pr_err("too many SCU I2C devices");
678 return;
679 }
680
681 new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
682 if (!new_dev) {
683 pr_err("failed to alloc mem for delayed i2c dev %s\n",
684 idev->type);
685 return;
686 }
687 memcpy(new_dev, idev, sizeof(*idev));
688
689 i2c_bus[i2c_next_dev] = bus;
690 i2c_devs[i2c_next_dev++] = new_dev;
691}
692
Alan Cox42c25442011-09-07 16:06:51 +0300693BLOCKING_NOTIFIER_HEAD(intel_scu_notifier);
694EXPORT_SYMBOL_GPL(intel_scu_notifier);
695
Feng Tang1da4b1c2010-11-09 11:22:58 +0000696/* Called by IPC driver */
697void intel_scu_devices_create(void)
698{
699 int i;
700
701 for (i = 0; i < ipc_next_dev; i++)
702 platform_device_add(ipc_devs[i]);
703
704 for (i = 0; i < spi_next_dev; i++)
705 spi_register_board_info(spi_devs[i], 1);
706
707 for (i = 0; i < i2c_next_dev; i++) {
708 struct i2c_adapter *adapter;
709 struct i2c_client *client;
710
711 adapter = i2c_get_adapter(i2c_bus[i]);
712 if (adapter) {
713 client = i2c_new_device(adapter, i2c_devs[i]);
714 if (!client)
715 pr_err("can't create i2c device %s\n",
716 i2c_devs[i]->type);
717 } else
718 i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
719 }
Alan Cox42c25442011-09-07 16:06:51 +0300720 intel_scu_notifier_post(SCU_AVAILABLE, 0L);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000721}
722EXPORT_SYMBOL_GPL(intel_scu_devices_create);
723
724/* Called by IPC driver */
725void intel_scu_devices_destroy(void)
726{
727 int i;
728
Alan Cox42c25442011-09-07 16:06:51 +0300729 intel_scu_notifier_post(SCU_DOWN, 0L);
730
Feng Tang1da4b1c2010-11-09 11:22:58 +0000731 for (i = 0; i < ipc_next_dev; i++)
732 platform_device_del(ipc_devs[i]);
733}
734EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
735
736static void __init install_irq_resource(struct platform_device *pdev, int irq)
737{
738 /* Single threaded */
739 static struct resource __initdata res = {
740 .name = "IRQ",
741 .flags = IORESOURCE_IRQ,
742 };
743 res.start = irq;
744 platform_device_add_resources(pdev, &res, 1);
745}
746
Mika Westerberg360545c2011-10-18 12:41:22 +0300747static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *entry)
Feng Tang1da4b1c2010-11-09 11:22:58 +0000748{
749 const struct devs_id *dev = device_ids;
Mika Westerberg360545c2011-10-18 12:41:22 +0300750 struct platform_device *pdev;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000751 void *pdata = NULL;
752
753 while (dev->name[0]) {
754 if (dev->type == SFI_DEV_TYPE_IPC &&
Mika Westerberg360545c2011-10-18 12:41:22 +0300755 !strncmp(dev->name, entry->name, SFI_NAME_LEN)) {
756 pdata = dev->get_platform_data(entry);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000757 break;
758 }
759 dev++;
760 }
Mika Westerberg360545c2011-10-18 12:41:22 +0300761
762 /*
763 * On Medfield the platform device creation is handled by the MSIC
764 * MFD driver so we don't need to do it here.
765 */
766 if (mrst_has_msic())
767 return;
768
769 /* ID as IRQ is a hack that will go away */
770 pdev = platform_device_alloc(entry->name, entry->irq);
771 if (pdev == NULL) {
772 pr_err("out of memory for SFI platform device '%s'.\n",
773 entry->name);
774 return;
775 }
776 install_irq_resource(pdev, entry->irq);
777
Feng Tang1da4b1c2010-11-09 11:22:58 +0000778 pdev->dev.platform_data = pdata;
779 intel_scu_device_register(pdev);
780}
781
782static void __init sfi_handle_spi_dev(struct spi_board_info *spi_info)
783{
784 const struct devs_id *dev = device_ids;
785 void *pdata = NULL;
786
787 while (dev->name[0]) {
788 if (dev->type == SFI_DEV_TYPE_SPI &&
789 !strncmp(dev->name, spi_info->modalias, SFI_NAME_LEN)) {
790 pdata = dev->get_platform_data(spi_info);
791 break;
792 }
793 dev++;
794 }
795 spi_info->platform_data = pdata;
796 if (dev->delay)
797 intel_scu_spi_device_register(spi_info);
798 else
799 spi_register_board_info(spi_info, 1);
800}
801
802static void __init sfi_handle_i2c_dev(int bus, struct i2c_board_info *i2c_info)
803{
804 const struct devs_id *dev = device_ids;
805 void *pdata = NULL;
806
807 while (dev->name[0]) {
808 if (dev->type == SFI_DEV_TYPE_I2C &&
809 !strncmp(dev->name, i2c_info->type, SFI_NAME_LEN)) {
810 pdata = dev->get_platform_data(i2c_info);
811 break;
812 }
813 dev++;
814 }
815 i2c_info->platform_data = pdata;
816
817 if (dev->delay)
818 intel_scu_i2c_device_register(bus, i2c_info);
819 else
820 i2c_register_board_info(bus, i2c_info, 1);
821 }
822
823
824static int __init sfi_parse_devs(struct sfi_table_header *table)
825{
826 struct sfi_table_simple *sb;
827 struct sfi_device_table_entry *pentry;
828 struct spi_board_info spi_info;
829 struct i2c_board_info i2c_info;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000830 int num, i, bus;
831 int ioapic;
832 struct io_apic_irq_attr irq_attr;
833
834 sb = (struct sfi_table_simple *)table;
835 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
836 pentry = (struct sfi_device_table_entry *)sb->pentry;
837
838 for (i = 0; i < num; i++, pentry++) {
Mika Westerberg153b19a2011-10-13 12:04:20 +0300839 int irq = pentry->irq;
840
841 if (irq != (u8)0xff) { /* native RTE case */
Feng Tang1da4b1c2010-11-09 11:22:58 +0000842 /* these SPI2 devices are not exposed to system as PCI
843 * devices, but they have separate RTE entry in IOAPIC
844 * so we have to enable them one by one here
845 */
Mika Westerberg153b19a2011-10-13 12:04:20 +0300846 ioapic = mp_find_ioapic(irq);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000847 irq_attr.ioapic = ioapic;
Mika Westerberg153b19a2011-10-13 12:04:20 +0300848 irq_attr.ioapic_pin = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000849 irq_attr.trigger = 1;
850 irq_attr.polarity = 1;
Mika Westerberg153b19a2011-10-13 12:04:20 +0300851 io_apic_set_pci_routing(NULL, irq, &irq_attr);
Kirill A. Shutemova94cc4e2011-08-26 12:20:59 +0100852 } else
Mika Westerberg153b19a2011-10-13 12:04:20 +0300853 irq = 0; /* No irq */
Kirill A. Shutemova94cc4e2011-08-26 12:20:59 +0100854
Feng Tang1da4b1c2010-11-09 11:22:58 +0000855 switch (pentry->type) {
856 case SFI_DEV_TYPE_IPC:
Feng Tang1da4b1c2010-11-09 11:22:58 +0000857 pr_debug("info[%2d]: IPC bus, name = %16.16s, "
Mika Westerberg360545c2011-10-18 12:41:22 +0300858 "irq = 0x%2x\n", i, pentry->name, pentry->irq);
859 sfi_handle_ipc_dev(pentry);
Feng Tang1da4b1c2010-11-09 11:22:58 +0000860 break;
861 case SFI_DEV_TYPE_SPI:
862 memset(&spi_info, 0, sizeof(spi_info));
863 strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
Mika Westerberg153b19a2011-10-13 12:04:20 +0300864 spi_info.irq = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000865 spi_info.bus_num = pentry->host_num;
866 spi_info.chip_select = pentry->addr;
867 spi_info.max_speed_hz = pentry->max_freq;
868 pr_debug("info[%2d]: SPI bus = %d, name = %16.16s, "
869 "irq = 0x%2x, max_freq = %d, cs = %d\n", i,
870 spi_info.bus_num,
871 spi_info.modalias,
872 spi_info.irq,
873 spi_info.max_speed_hz,
874 spi_info.chip_select);
875 sfi_handle_spi_dev(&spi_info);
876 break;
877 case SFI_DEV_TYPE_I2C:
878 memset(&i2c_info, 0, sizeof(i2c_info));
879 bus = pentry->host_num;
880 strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
Mika Westerberg153b19a2011-10-13 12:04:20 +0300881 i2c_info.irq = irq;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000882 i2c_info.addr = pentry->addr;
883 pr_debug("info[%2d]: I2C bus = %d, name = %16.16s, "
884 "irq = 0x%2x, addr = 0x%x\n", i, bus,
885 i2c_info.type,
886 i2c_info.irq,
887 i2c_info.addr);
888 sfi_handle_i2c_dev(bus, &i2c_info);
889 break;
890 case SFI_DEV_TYPE_UART:
891 case SFI_DEV_TYPE_HSI:
892 default:
893 ;
894 }
895 }
896 return 0;
897}
898
899static int __init mrst_platform_init(void)
900{
901 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
902 sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
903 return 0;
904}
905arch_initcall(mrst_platform_init);
906
907/*
908 * we will search these buttons in SFI GPIO table (by name)
909 * and register them dynamically. Please add all possible
910 * buttons here, we will shrink them if no GPIO found.
911 */
912static struct gpio_keys_button gpio_button[] = {
913 {KEY_POWER, -1, 1, "power_btn", EV_KEY, 0, 3000},
914 {KEY_PROG1, -1, 1, "prog_btn1", EV_KEY, 0, 20},
915 {KEY_PROG2, -1, 1, "prog_btn2", EV_KEY, 0, 20},
916 {SW_LID, -1, 1, "lid_switch", EV_SW, 0, 20},
917 {KEY_VOLUMEUP, -1, 1, "vol_up", EV_KEY, 0, 20},
918 {KEY_VOLUMEDOWN, -1, 1, "vol_down", EV_KEY, 0, 20},
919 {KEY_CAMERA, -1, 1, "camera_full", EV_KEY, 0, 20},
920 {KEY_CAMERA_FOCUS, -1, 1, "camera_half", EV_KEY, 0, 20},
921 {SW_KEYPAD_SLIDE, -1, 1, "MagSw1", EV_SW, 0, 20},
922 {SW_KEYPAD_SLIDE, -1, 1, "MagSw2", EV_SW, 0, 20},
923};
924
925static struct gpio_keys_platform_data mrst_gpio_keys = {
926 .buttons = gpio_button,
927 .rep = 1,
928 .nbuttons = -1, /* will fill it after search */
929};
930
931static struct platform_device pb_device = {
932 .name = "gpio-keys",
933 .id = -1,
934 .dev = {
935 .platform_data = &mrst_gpio_keys,
936 },
937};
938
939/*
940 * Shrink the non-existent buttons, register the gpio button
941 * device if there is some
942 */
943static int __init pb_keys_init(void)
944{
945 struct gpio_keys_button *gb = gpio_button;
946 int i, num, good = 0;
947
948 num = sizeof(gpio_button) / sizeof(struct gpio_keys_button);
949 for (i = 0; i < num; i++) {
950 gb[i].gpio = get_gpio_by_name(gb[i].desc);
951 if (gb[i].gpio == -1)
952 continue;
953
954 if (i != good)
955 gb[good] = gb[i];
956 good++;
957 }
958
959 if (good) {
960 mrst_gpio_keys.nbuttons = good;
961 return platform_device_register(&pb_device);
962 }
963 return 0;
964}
965late_initcall(pb_keys_init);