PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/x86/platform/intel-mid/sfi.c

https://bitbucket.org/mirror/linux
C | 543 lines | 436 code | 72 blank | 35 comment | 60 complexity | 8ff8b8f6d6895f15f8ce6a1ff2085458 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * intel_mid_sfi.c: Intel MID SFI initialization code
  4. *
  5. * (C) Copyright 2013 Intel Corporation
  6. * Author: Sathyanarayanan Kuppuswamy <sathyanarayanan.kuppuswamy@intel.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/sfi.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/i2c.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/gpio.h>
  17. #include <linux/gpio_keys.h>
  18. #include <linux/input.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/irq.h>
  21. #include <linux/export.h>
  22. #include <linux/notifier.h>
  23. #include <linux/mmc/core.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/blkdev.h>
  26. #include <asm/setup.h>
  27. #include <asm/mpspec_def.h>
  28. #include <asm/hw_irq.h>
  29. #include <asm/apic.h>
  30. #include <asm/io_apic.h>
  31. #include <asm/intel-mid.h>
  32. #include <asm/intel_mid_vrtc.h>
  33. #include <asm/io.h>
  34. #include <asm/i8259.h>
  35. #include <asm/intel_scu_ipc.h>
  36. #include <asm/apb_timer.h>
  37. #include <asm/reboot.h>
  38. #define SFI_SIG_OEM0 "OEM0"
  39. #define MAX_IPCDEVS 24
  40. #define MAX_SCU_SPI 24
  41. #define MAX_SCU_I2C 24
  42. static struct platform_device *ipc_devs[MAX_IPCDEVS];
  43. static struct spi_board_info *spi_devs[MAX_SCU_SPI];
  44. static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
  45. static struct sfi_gpio_table_entry *gpio_table;
  46. static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
  47. static int ipc_next_dev;
  48. static int spi_next_dev;
  49. static int i2c_next_dev;
  50. static int i2c_bus[MAX_SCU_I2C];
  51. static int gpio_num_entry;
  52. static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
  53. int sfi_mrtc_num;
  54. int sfi_mtimer_num;
  55. struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
  56. EXPORT_SYMBOL_GPL(sfi_mrtc_array);
  57. struct blocking_notifier_head intel_scu_notifier =
  58. BLOCKING_NOTIFIER_INIT(intel_scu_notifier);
  59. EXPORT_SYMBOL_GPL(intel_scu_notifier);
  60. #define intel_mid_sfi_get_pdata(dev, priv) \
  61. ((dev)->get_platform_data ? (dev)->get_platform_data(priv) : NULL)
  62. /* parse all the mtimer info to a static mtimer array */
  63. int __init sfi_parse_mtmr(struct sfi_table_header *table)
  64. {
  65. struct sfi_table_simple *sb;
  66. struct sfi_timer_table_entry *pentry;
  67. struct mpc_intsrc mp_irq;
  68. int totallen;
  69. sb = (struct sfi_table_simple *)table;
  70. if (!sfi_mtimer_num) {
  71. sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
  72. struct sfi_timer_table_entry);
  73. pentry = (struct sfi_timer_table_entry *) sb->pentry;
  74. totallen = sfi_mtimer_num * sizeof(*pentry);
  75. memcpy(sfi_mtimer_array, pentry, totallen);
  76. }
  77. pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
  78. pentry = sfi_mtimer_array;
  79. for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
  80. pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz, irq = %d\n",
  81. totallen, (u32)pentry->phys_addr,
  82. pentry->freq_hz, pentry->irq);
  83. mp_irq.type = MP_INTSRC;
  84. mp_irq.irqtype = mp_INT;
  85. mp_irq.irqflag = MP_IRQTRIG_EDGE | MP_IRQPOL_ACTIVE_HIGH;
  86. mp_irq.srcbus = MP_BUS_ISA;
  87. mp_irq.srcbusirq = pentry->irq; /* IRQ */
  88. mp_irq.dstapic = MP_APIC_ALL;
  89. mp_irq.dstirq = pentry->irq;
  90. mp_save_irq(&mp_irq);
  91. mp_map_gsi_to_irq(pentry->irq, IOAPIC_MAP_ALLOC, NULL);
  92. }
  93. return 0;
  94. }
  95. struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
  96. {
  97. int i;
  98. if (hint < sfi_mtimer_num) {
  99. if (!sfi_mtimer_usage[hint]) {
  100. pr_debug("hint taken for timer %d irq %d\n",
  101. hint, sfi_mtimer_array[hint].irq);
  102. sfi_mtimer_usage[hint] = 1;
  103. return &sfi_mtimer_array[hint];
  104. }
  105. }
  106. /* take the first timer available */
  107. for (i = 0; i < sfi_mtimer_num;) {
  108. if (!sfi_mtimer_usage[i]) {
  109. sfi_mtimer_usage[i] = 1;
  110. return &sfi_mtimer_array[i];
  111. }
  112. i++;
  113. }
  114. return NULL;
  115. }
  116. void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
  117. {
  118. int i;
  119. for (i = 0; i < sfi_mtimer_num;) {
  120. if (mtmr->irq == sfi_mtimer_array[i].irq) {
  121. sfi_mtimer_usage[i] = 0;
  122. return;
  123. }
  124. i++;
  125. }
  126. }
  127. /* parse all the mrtc info to a global mrtc array */
  128. int __init sfi_parse_mrtc(struct sfi_table_header *table)
  129. {
  130. struct sfi_table_simple *sb;
  131. struct sfi_rtc_table_entry *pentry;
  132. struct mpc_intsrc mp_irq;
  133. int totallen;
  134. sb = (struct sfi_table_simple *)table;
  135. if (!sfi_mrtc_num) {
  136. sfi_mrtc_num = SFI_GET_NUM_ENTRIES(sb,
  137. struct sfi_rtc_table_entry);
  138. pentry = (struct sfi_rtc_table_entry *)sb->pentry;
  139. totallen = sfi_mrtc_num * sizeof(*pentry);
  140. memcpy(sfi_mrtc_array, pentry, totallen);
  141. }
  142. pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
  143. pentry = sfi_mrtc_array;
  144. for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
  145. pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
  146. totallen, (u32)pentry->phys_addr, pentry->irq);
  147. mp_irq.type = MP_INTSRC;
  148. mp_irq.irqtype = mp_INT;
  149. mp_irq.irqflag = MP_IRQTRIG_LEVEL | MP_IRQPOL_ACTIVE_LOW;
  150. mp_irq.srcbus = MP_BUS_ISA;
  151. mp_irq.srcbusirq = pentry->irq; /* IRQ */
  152. mp_irq.dstapic = MP_APIC_ALL;
  153. mp_irq.dstirq = pentry->irq;
  154. mp_save_irq(&mp_irq);
  155. mp_map_gsi_to_irq(pentry->irq, IOAPIC_MAP_ALLOC, NULL);
  156. }
  157. return 0;
  158. }
  159. /*
  160. * Parsing GPIO table first, since the DEVS table will need this table
  161. * to map the pin name to the actual pin.
  162. */
  163. static int __init sfi_parse_gpio(struct sfi_table_header *table)
  164. {
  165. struct sfi_table_simple *sb;
  166. struct sfi_gpio_table_entry *pentry;
  167. int num, i;
  168. if (gpio_table)
  169. return 0;
  170. sb = (struct sfi_table_simple *)table;
  171. num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
  172. pentry = (struct sfi_gpio_table_entry *)sb->pentry;
  173. gpio_table = kmemdup(pentry, num * sizeof(*pentry), GFP_KERNEL);
  174. if (!gpio_table)
  175. return -1;
  176. gpio_num_entry = num;
  177. pr_debug("GPIO pin info:\n");
  178. for (i = 0; i < num; i++, pentry++)
  179. pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
  180. " pin = %d\n", i,
  181. pentry->controller_name,
  182. pentry->pin_name,
  183. pentry->pin_no);
  184. return 0;
  185. }
  186. int get_gpio_by_name(const char *name)
  187. {
  188. struct sfi_gpio_table_entry *pentry = gpio_table;
  189. int i;
  190. if (!pentry)
  191. return -1;
  192. for (i = 0; i < gpio_num_entry; i++, pentry++) {
  193. if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
  194. return pentry->pin_no;
  195. }
  196. return -EINVAL;
  197. }
  198. static void __init intel_scu_ipc_device_register(struct platform_device *pdev)
  199. {
  200. if (ipc_next_dev == MAX_IPCDEVS)
  201. pr_err("too many SCU IPC devices");
  202. else
  203. ipc_devs[ipc_next_dev++] = pdev;
  204. }
  205. static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
  206. {
  207. struct spi_board_info *new_dev;
  208. if (spi_next_dev == MAX_SCU_SPI) {
  209. pr_err("too many SCU SPI devices");
  210. return;
  211. }
  212. new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  213. if (!new_dev) {
  214. pr_err("failed to alloc mem for delayed spi dev %s\n",
  215. sdev->modalias);
  216. return;
  217. }
  218. *new_dev = *sdev;
  219. spi_devs[spi_next_dev++] = new_dev;
  220. }
  221. static void __init intel_scu_i2c_device_register(int bus,
  222. struct i2c_board_info *idev)
  223. {
  224. struct i2c_board_info *new_dev;
  225. if (i2c_next_dev == MAX_SCU_I2C) {
  226. pr_err("too many SCU I2C devices");
  227. return;
  228. }
  229. new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
  230. if (!new_dev) {
  231. pr_err("failed to alloc mem for delayed i2c dev %s\n",
  232. idev->type);
  233. return;
  234. }
  235. *new_dev = *idev;
  236. i2c_bus[i2c_next_dev] = bus;
  237. i2c_devs[i2c_next_dev++] = new_dev;
  238. }
  239. /* Called by IPC driver */
  240. void intel_scu_devices_create(void)
  241. {
  242. int i;
  243. for (i = 0; i < ipc_next_dev; i++)
  244. platform_device_add(ipc_devs[i]);
  245. for (i = 0; i < spi_next_dev; i++)
  246. spi_register_board_info(spi_devs[i], 1);
  247. for (i = 0; i < i2c_next_dev; i++) {
  248. struct i2c_adapter *adapter;
  249. struct i2c_client *client;
  250. adapter = i2c_get_adapter(i2c_bus[i]);
  251. if (adapter) {
  252. client = i2c_new_client_device(adapter, i2c_devs[i]);
  253. if (IS_ERR(client))
  254. pr_err("can't create i2c device %s\n",
  255. i2c_devs[i]->type);
  256. } else
  257. i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
  258. }
  259. intel_scu_notifier_post(SCU_AVAILABLE, NULL);
  260. }
  261. EXPORT_SYMBOL_GPL(intel_scu_devices_create);
  262. /* Called by IPC driver */
  263. void intel_scu_devices_destroy(void)
  264. {
  265. int i;
  266. intel_scu_notifier_post(SCU_DOWN, NULL);
  267. for (i = 0; i < ipc_next_dev; i++)
  268. platform_device_del(ipc_devs[i]);
  269. }
  270. EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
  271. static void __init install_irq_resource(struct platform_device *pdev, int irq)
  272. {
  273. /* Single threaded */
  274. static struct resource res __initdata = {
  275. .name = "IRQ",
  276. .flags = IORESOURCE_IRQ,
  277. };
  278. res.start = irq;
  279. platform_device_add_resources(pdev, &res, 1);
  280. }
  281. static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *pentry,
  282. struct devs_id *dev)
  283. {
  284. struct platform_device *pdev;
  285. void *pdata = NULL;
  286. pr_debug("IPC bus, name = %16.16s, irq = 0x%2x\n",
  287. pentry->name, pentry->irq);
  288. /*
  289. * We need to call platform init of IPC devices to fill misc_pdata
  290. * structure. It will be used in msic_init for initialization.
  291. */
  292. pdata = intel_mid_sfi_get_pdata(dev, pentry);
  293. if (IS_ERR(pdata))
  294. return;
  295. /*
  296. * On Medfield the platform device creation is handled by the MSIC
  297. * MFD driver so we don't need to do it here.
  298. */
  299. if (dev->msic && intel_mid_has_msic())
  300. return;
  301. pdev = platform_device_alloc(pentry->name, 0);
  302. if (pdev == NULL) {
  303. pr_err("out of memory for SFI platform device '%s'.\n",
  304. pentry->name);
  305. return;
  306. }
  307. install_irq_resource(pdev, pentry->irq);
  308. pdev->dev.platform_data = pdata;
  309. if (dev->delay)
  310. intel_scu_ipc_device_register(pdev);
  311. else
  312. platform_device_add(pdev);
  313. }
  314. static void __init sfi_handle_spi_dev(struct sfi_device_table_entry *pentry,
  315. struct devs_id *dev)
  316. {
  317. struct spi_board_info spi_info;
  318. void *pdata = NULL;
  319. memset(&spi_info, 0, sizeof(spi_info));
  320. strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
  321. spi_info.irq = ((pentry->irq == (u8)0xff) ? 0 : pentry->irq);
  322. spi_info.bus_num = pentry->host_num;
  323. spi_info.chip_select = pentry->addr;
  324. spi_info.max_speed_hz = pentry->max_freq;
  325. pr_debug("SPI bus=%d, name=%16.16s, irq=0x%2x, max_freq=%d, cs=%d\n",
  326. spi_info.bus_num,
  327. spi_info.modalias,
  328. spi_info.irq,
  329. spi_info.max_speed_hz,
  330. spi_info.chip_select);
  331. pdata = intel_mid_sfi_get_pdata(dev, &spi_info);
  332. if (IS_ERR(pdata))
  333. return;
  334. spi_info.platform_data = pdata;
  335. if (dev->delay)
  336. intel_scu_spi_device_register(&spi_info);
  337. else
  338. spi_register_board_info(&spi_info, 1);
  339. }
  340. static void __init sfi_handle_i2c_dev(struct sfi_device_table_entry *pentry,
  341. struct devs_id *dev)
  342. {
  343. struct i2c_board_info i2c_info;
  344. void *pdata = NULL;
  345. memset(&i2c_info, 0, sizeof(i2c_info));
  346. strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
  347. i2c_info.irq = ((pentry->irq == (u8)0xff) ? 0 : pentry->irq);
  348. i2c_info.addr = pentry->addr;
  349. pr_debug("I2C bus = %d, name = %16.16s, irq = 0x%2x, addr = 0x%x\n",
  350. pentry->host_num,
  351. i2c_info.type,
  352. i2c_info.irq,
  353. i2c_info.addr);
  354. pdata = intel_mid_sfi_get_pdata(dev, &i2c_info);
  355. i2c_info.platform_data = pdata;
  356. if (IS_ERR(pdata))
  357. return;
  358. if (dev->delay)
  359. intel_scu_i2c_device_register(pentry->host_num, &i2c_info);
  360. else
  361. i2c_register_board_info(pentry->host_num, &i2c_info, 1);
  362. }
  363. static void __init sfi_handle_sd_dev(struct sfi_device_table_entry *pentry,
  364. struct devs_id *dev)
  365. {
  366. struct mid_sd_board_info sd_info;
  367. void *pdata;
  368. memset(&sd_info, 0, sizeof(sd_info));
  369. strncpy(sd_info.name, pentry->name, SFI_NAME_LEN);
  370. sd_info.bus_num = pentry->host_num;
  371. sd_info.max_clk = pentry->max_freq;
  372. sd_info.addr = pentry->addr;
  373. pr_debug("SD bus = %d, name = %16.16s, max_clk = %d, addr = 0x%x\n",
  374. sd_info.bus_num,
  375. sd_info.name,
  376. sd_info.max_clk,
  377. sd_info.addr);
  378. pdata = intel_mid_sfi_get_pdata(dev, &sd_info);
  379. if (IS_ERR(pdata))
  380. return;
  381. /* Nothing we can do with this for now */
  382. sd_info.platform_data = pdata;
  383. pr_debug("Successfully registered %16.16s", sd_info.name);
  384. }
  385. extern struct devs_id *const __x86_intel_mid_dev_start[],
  386. *const __x86_intel_mid_dev_end[];
  387. static struct devs_id __init *get_device_id(u8 type, char *name)
  388. {
  389. struct devs_id *const *dev_table;
  390. for (dev_table = __x86_intel_mid_dev_start;
  391. dev_table < __x86_intel_mid_dev_end; dev_table++) {
  392. struct devs_id *dev = *dev_table;
  393. if (dev->type == type &&
  394. !strncmp(dev->name, name, SFI_NAME_LEN)) {
  395. return dev;
  396. }
  397. }
  398. return NULL;
  399. }
  400. static int __init sfi_parse_devs(struct sfi_table_header *table)
  401. {
  402. struct sfi_table_simple *sb;
  403. struct sfi_device_table_entry *pentry;
  404. struct devs_id *dev = NULL;
  405. int num, i, ret;
  406. int polarity;
  407. struct irq_alloc_info info;
  408. sb = (struct sfi_table_simple *)table;
  409. num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
  410. pentry = (struct sfi_device_table_entry *)sb->pentry;
  411. for (i = 0; i < num; i++, pentry++) {
  412. int irq = pentry->irq;
  413. if (irq != (u8)0xff) { /* native RTE case */
  414. /* these SPI2 devices are not exposed to system as PCI
  415. * devices, but they have separate RTE entry in IOAPIC
  416. * so we have to enable them one by one here
  417. */
  418. if (intel_mid_identify_cpu() ==
  419. INTEL_MID_CPU_CHIP_TANGIER) {
  420. if (!strncmp(pentry->name, "r69001-ts-i2c", 13))
  421. /* active low */
  422. polarity = 1;
  423. else if (!strncmp(pentry->name,
  424. "synaptics_3202", 14))
  425. /* active low */
  426. polarity = 1;
  427. else if (irq == 41)
  428. /* fast_int_1 */
  429. polarity = 1;
  430. else
  431. /* active high */
  432. polarity = 0;
  433. } else {
  434. /* PNW and CLV go with active low */
  435. polarity = 1;
  436. }
  437. ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 1, polarity);
  438. ret = mp_map_gsi_to_irq(irq, IOAPIC_MAP_ALLOC, &info);
  439. WARN_ON(ret < 0);
  440. }
  441. dev = get_device_id(pentry->type, pentry->name);
  442. if (!dev)
  443. continue;
  444. switch (pentry->type) {
  445. case SFI_DEV_TYPE_IPC:
  446. sfi_handle_ipc_dev(pentry, dev);
  447. break;
  448. case SFI_DEV_TYPE_SPI:
  449. sfi_handle_spi_dev(pentry, dev);
  450. break;
  451. case SFI_DEV_TYPE_I2C:
  452. sfi_handle_i2c_dev(pentry, dev);
  453. break;
  454. case SFI_DEV_TYPE_SD:
  455. sfi_handle_sd_dev(pentry, dev);
  456. break;
  457. case SFI_DEV_TYPE_UART:
  458. case SFI_DEV_TYPE_HSI:
  459. default:
  460. break;
  461. }
  462. }
  463. return 0;
  464. }
  465. static int __init intel_mid_platform_init(void)
  466. {
  467. sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
  468. sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
  469. return 0;
  470. }
  471. arch_initcall(intel_mid_platform_init);