sfi.c 12 KB

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