2 * intel_mid_sfi.c: Intel MID SFI initialization code
4 * (C) Copyright 2013 Intel Corporation
5 * Author: Sathyanarayanan Kuppuswamy <sathyanarayanan.kuppuswamy@intel.com>
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
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/interrupt.h>
16 #include <linux/scatterlist.h>
17 #include <linux/sfi.h>
18 #include <linux/intel_pmic_gpio.h>
19 #include <linux/spi/spi.h>
20 #include <linux/i2c.h>
21 #include <linux/skbuff.h>
22 #include <linux/gpio.h>
23 #include <linux/gpio_keys.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/irq.h>
27 #include <linux/export.h>
28 #include <linux/notifier.h>
29 #include <linux/mmc/core.h>
30 #include <linux/mmc/card.h>
31 #include <linux/blkdev.h>
33 #include <asm/setup.h>
34 #include <asm/mpspec_def.h>
35 #include <asm/hw_irq.h>
37 #include <asm/io_apic.h>
38 #include <asm/intel-mid.h>
39 #include <asm/intel_mid_vrtc.h>
41 #include <asm/i8259.h>
42 #include <asm/intel_scu_ipc.h>
43 #include <asm/apb_timer.h>
44 #include <asm/reboot.h>
46 #define SFI_SIG_OEM0 "OEM0"
47 #define MAX_IPCDEVS 24
48 #define MAX_SCU_SPI 24
49 #define MAX_SCU_I2C 24
51 static struct platform_device *ipc_devs[MAX_IPCDEVS];
52 static struct spi_board_info *spi_devs[MAX_SCU_SPI];
53 static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
54 static struct sfi_gpio_table_entry *gpio_table;
55 static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
56 static int ipc_next_dev;
57 static int spi_next_dev;
58 static int i2c_next_dev;
59 static int i2c_bus[MAX_SCU_I2C];
60 static int gpio_num_entry;
61 static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
65 struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
66 EXPORT_SYMBOL_GPL(sfi_mrtc_array);
68 struct blocking_notifier_head intel_scu_notifier =
69 BLOCKING_NOTIFIER_INIT(intel_scu_notifier);
70 EXPORT_SYMBOL_GPL(intel_scu_notifier);
72 #define intel_mid_sfi_get_pdata(dev, priv) \
73 ((dev)->get_platform_data ? (dev)->get_platform_data(priv) : NULL)
75 /* parse all the mtimer info to a static mtimer array */
76 int __init sfi_parse_mtmr(struct sfi_table_header *table)
78 struct sfi_table_simple *sb;
79 struct sfi_timer_table_entry *pentry;
80 struct mpc_intsrc mp_irq;
83 sb = (struct sfi_table_simple *)table;
84 if (!sfi_mtimer_num) {
85 sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
86 struct sfi_timer_table_entry);
87 pentry = (struct sfi_timer_table_entry *) sb->pentry;
88 totallen = sfi_mtimer_num * sizeof(*pentry);
89 memcpy(sfi_mtimer_array, pentry, totallen);
92 pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
93 pentry = sfi_mtimer_array;
94 for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
95 pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz, irq = %d\n",
96 totallen, (u32)pentry->phys_addr,
97 pentry->freq_hz, pentry->irq);
98 mp_irq.type = MP_INTSRC;
99 mp_irq.irqtype = mp_INT;
100 /* triggering mode edge bit 2-3, active high polarity bit 0-1 */
102 mp_irq.srcbus = MP_BUS_ISA;
103 mp_irq.srcbusirq = pentry->irq; /* IRQ */
104 mp_irq.dstapic = MP_APIC_ALL;
105 mp_irq.dstirq = pentry->irq;
106 mp_save_irq(&mp_irq);
107 mp_map_gsi_to_irq(pentry->irq, IOAPIC_MAP_ALLOC, NULL);
113 struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
116 if (hint < sfi_mtimer_num) {
117 if (!sfi_mtimer_usage[hint]) {
118 pr_debug("hint taken for timer %d irq %d\n",
119 hint, sfi_mtimer_array[hint].irq);
120 sfi_mtimer_usage[hint] = 1;
121 return &sfi_mtimer_array[hint];
124 /* take the first timer available */
125 for (i = 0; i < sfi_mtimer_num;) {
126 if (!sfi_mtimer_usage[i]) {
127 sfi_mtimer_usage[i] = 1;
128 return &sfi_mtimer_array[i];
135 void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
138 for (i = 0; i < sfi_mtimer_num;) {
139 if (mtmr->irq == sfi_mtimer_array[i].irq) {
140 sfi_mtimer_usage[i] = 0;
147 /* parse all the mrtc info to a global mrtc array */
148 int __init sfi_parse_mrtc(struct sfi_table_header *table)
150 struct sfi_table_simple *sb;
151 struct sfi_rtc_table_entry *pentry;
152 struct mpc_intsrc mp_irq;
156 sb = (struct sfi_table_simple *)table;
158 sfi_mrtc_num = SFI_GET_NUM_ENTRIES(sb,
159 struct sfi_rtc_table_entry);
160 pentry = (struct sfi_rtc_table_entry *)sb->pentry;
161 totallen = sfi_mrtc_num * sizeof(*pentry);
162 memcpy(sfi_mrtc_array, pentry, totallen);
165 pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
166 pentry = sfi_mrtc_array;
167 for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
168 pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
169 totallen, (u32)pentry->phys_addr, pentry->irq);
170 mp_irq.type = MP_INTSRC;
171 mp_irq.irqtype = mp_INT;
172 mp_irq.irqflag = 0xf; /* level trigger and active low */
173 mp_irq.srcbus = MP_BUS_ISA;
174 mp_irq.srcbusirq = pentry->irq; /* IRQ */
175 mp_irq.dstapic = MP_APIC_ALL;
176 mp_irq.dstirq = pentry->irq;
177 mp_save_irq(&mp_irq);
178 mp_map_gsi_to_irq(pentry->irq, IOAPIC_MAP_ALLOC, NULL);
185 * Parsing GPIO table first, since the DEVS table will need this table
186 * to map the pin name to the actual pin.
188 static int __init sfi_parse_gpio(struct sfi_table_header *table)
190 struct sfi_table_simple *sb;
191 struct sfi_gpio_table_entry *pentry;
196 sb = (struct sfi_table_simple *)table;
197 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
198 pentry = (struct sfi_gpio_table_entry *)sb->pentry;
200 gpio_table = kmemdup(pentry, num * sizeof(*pentry), GFP_KERNEL);
203 gpio_num_entry = num;
205 pr_debug("GPIO pin info:\n");
206 for (i = 0; i < num; i++, pentry++)
207 pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
209 pentry->controller_name,
215 int get_gpio_by_name(const char *name)
217 struct sfi_gpio_table_entry *pentry = gpio_table;
222 for (i = 0; i < gpio_num_entry; i++, pentry++) {
223 if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
224 return pentry->pin_no;
229 static void __init intel_scu_ipc_device_register(struct platform_device *pdev)
231 if (ipc_next_dev == MAX_IPCDEVS)
232 pr_err("too many SCU IPC devices");
234 ipc_devs[ipc_next_dev++] = pdev;
237 static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
239 struct spi_board_info *new_dev;
241 if (spi_next_dev == MAX_SCU_SPI) {
242 pr_err("too many SCU SPI devices");
246 new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
248 pr_err("failed to alloc mem for delayed spi dev %s\n",
254 spi_devs[spi_next_dev++] = new_dev;
257 static void __init intel_scu_i2c_device_register(int bus,
258 struct i2c_board_info *idev)
260 struct i2c_board_info *new_dev;
262 if (i2c_next_dev == MAX_SCU_I2C) {
263 pr_err("too many SCU I2C devices");
267 new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
269 pr_err("failed to alloc mem for delayed i2c dev %s\n",
275 i2c_bus[i2c_next_dev] = bus;
276 i2c_devs[i2c_next_dev++] = new_dev;
279 /* Called by IPC driver */
280 void intel_scu_devices_create(void)
284 for (i = 0; i < ipc_next_dev; i++)
285 platform_device_add(ipc_devs[i]);
287 for (i = 0; i < spi_next_dev; i++)
288 spi_register_board_info(spi_devs[i], 1);
290 for (i = 0; i < i2c_next_dev; i++) {
291 struct i2c_adapter *adapter;
292 struct i2c_client *client;
294 adapter = i2c_get_adapter(i2c_bus[i]);
296 client = i2c_new_device(adapter, i2c_devs[i]);
298 pr_err("can't create i2c device %s\n",
301 i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
303 intel_scu_notifier_post(SCU_AVAILABLE, NULL);
305 EXPORT_SYMBOL_GPL(intel_scu_devices_create);
307 /* Called by IPC driver */
308 void intel_scu_devices_destroy(void)
312 intel_scu_notifier_post(SCU_DOWN, NULL);
314 for (i = 0; i < ipc_next_dev; i++)
315 platform_device_del(ipc_devs[i]);
317 EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
319 static void __init install_irq_resource(struct platform_device *pdev, int irq)
321 /* Single threaded */
322 static struct resource res __initdata = {
324 .flags = IORESOURCE_IRQ,
327 platform_device_add_resources(pdev, &res, 1);
330 static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *pentry,
333 struct platform_device *pdev;
336 pr_debug("IPC bus, name = %16.16s, irq = 0x%2x\n",
337 pentry->name, pentry->irq);
340 * We need to call platform init of IPC devices to fill misc_pdata
341 * structure. It will be used in msic_init for initialization.
343 pdata = intel_mid_sfi_get_pdata(dev, pentry);
348 * On Medfield the platform device creation is handled by the MSIC
349 * MFD driver so we don't need to do it here.
351 if (dev->msic && intel_mid_has_msic())
354 pdev = platform_device_alloc(pentry->name, 0);
356 pr_err("out of memory for SFI platform device '%s'.\n",
360 install_irq_resource(pdev, pentry->irq);
362 pdev->dev.platform_data = pdata;
364 intel_scu_ipc_device_register(pdev);
366 platform_device_add(pdev);
369 static void __init sfi_handle_spi_dev(struct sfi_device_table_entry *pentry,
372 struct spi_board_info spi_info;
375 memset(&spi_info, 0, sizeof(spi_info));
376 strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
377 spi_info.irq = ((pentry->irq == (u8)0xff) ? 0 : pentry->irq);
378 spi_info.bus_num = pentry->host_num;
379 spi_info.chip_select = pentry->addr;
380 spi_info.max_speed_hz = pentry->max_freq;
381 pr_debug("SPI bus=%d, name=%16.16s, irq=0x%2x, max_freq=%d, cs=%d\n",
385 spi_info.max_speed_hz,
386 spi_info.chip_select);
388 pdata = intel_mid_sfi_get_pdata(dev, &spi_info);
392 spi_info.platform_data = pdata;
394 intel_scu_spi_device_register(&spi_info);
396 spi_register_board_info(&spi_info, 1);
399 static void __init sfi_handle_i2c_dev(struct sfi_device_table_entry *pentry,
402 struct i2c_board_info i2c_info;
405 memset(&i2c_info, 0, sizeof(i2c_info));
406 strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
407 i2c_info.irq = ((pentry->irq == (u8)0xff) ? 0 : pentry->irq);
408 i2c_info.addr = pentry->addr;
409 pr_debug("I2C bus = %d, name = %16.16s, irq = 0x%2x, addr = 0x%x\n",
414 pdata = intel_mid_sfi_get_pdata(dev, &i2c_info);
415 i2c_info.platform_data = pdata;
420 intel_scu_i2c_device_register(pentry->host_num, &i2c_info);
422 i2c_register_board_info(pentry->host_num, &i2c_info, 1);
425 static void __init sfi_handle_sd_dev(struct sfi_device_table_entry *pentry,
428 struct mid_sd_board_info sd_info;
431 memset(&sd_info, 0, sizeof(sd_info));
432 strncpy(sd_info.name, pentry->name, SFI_NAME_LEN);
433 sd_info.bus_num = pentry->host_num;
434 sd_info.max_clk = pentry->max_freq;
435 sd_info.addr = pentry->addr;
436 pr_debug("SD bus = %d, name = %16.16s, max_clk = %d, addr = 0x%x\n",
441 pdata = intel_mid_sfi_get_pdata(dev, &sd_info);
445 /* Nothing we can do with this for now */
446 sd_info.platform_data = pdata;
448 pr_debug("Successfully registered %16.16s", sd_info.name);
451 extern struct devs_id *const __x86_intel_mid_dev_start[],
452 *const __x86_intel_mid_dev_end[];
454 static struct devs_id __init *get_device_id(u8 type, char *name)
456 struct devs_id *const *dev_table;
458 for (dev_table = __x86_intel_mid_dev_start;
459 dev_table < __x86_intel_mid_dev_end; dev_table++) {
460 struct devs_id *dev = *dev_table;
461 if (dev->type == type &&
462 !strncmp(dev->name, name, SFI_NAME_LEN)) {
470 static int __init sfi_parse_devs(struct sfi_table_header *table)
472 struct sfi_table_simple *sb;
473 struct sfi_device_table_entry *pentry;
474 struct devs_id *dev = NULL;
477 struct irq_alloc_info info;
479 sb = (struct sfi_table_simple *)table;
480 num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
481 pentry = (struct sfi_device_table_entry *)sb->pentry;
483 for (i = 0; i < num; i++, pentry++) {
484 int irq = pentry->irq;
486 if (irq != (u8)0xff) { /* native RTE case */
487 /* these SPI2 devices are not exposed to system as PCI
488 * devices, but they have separate RTE entry in IOAPIC
489 * so we have to enable them one by one here
491 if (intel_mid_identify_cpu() ==
492 INTEL_MID_CPU_CHIP_TANGIER) {
493 if (!strncmp(pentry->name, "r69001-ts-i2c", 13))
496 else if (!strncmp(pentry->name,
497 "synaptics_3202", 14))
507 /* PNW and CLV go with active low */
511 ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 1, polarity);
512 ret = mp_map_gsi_to_irq(irq, IOAPIC_MAP_ALLOC, &info);
516 dev = get_device_id(pentry->type, pentry->name);
521 switch (pentry->type) {
522 case SFI_DEV_TYPE_IPC:
523 sfi_handle_ipc_dev(pentry, dev);
525 case SFI_DEV_TYPE_SPI:
526 sfi_handle_spi_dev(pentry, dev);
528 case SFI_DEV_TYPE_I2C:
529 sfi_handle_i2c_dev(pentry, dev);
531 case SFI_DEV_TYPE_SD:
532 sfi_handle_sd_dev(pentry, dev);
534 case SFI_DEV_TYPE_UART:
535 case SFI_DEV_TYPE_HSI:
543 static int __init intel_mid_platform_init(void)
545 sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
546 sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
549 arch_initcall(intel_mid_platform_init);