mrst.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  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. */
  12. #define pr_fmt(fmt) "mrst: " fmt
  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/i2c/pca953x.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/mfd/intel_msic.h>
  29. #include <asm/setup.h>
  30. #include <asm/mpspec_def.h>
  31. #include <asm/hw_irq.h>
  32. #include <asm/apic.h>
  33. #include <asm/io_apic.h>
  34. #include <asm/mrst.h>
  35. #include <asm/mrst-vrtc.h>
  36. #include <asm/io.h>
  37. #include <asm/i8259.h>
  38. #include <asm/intel_scu_ipc.h>
  39. #include <asm/apb_timer.h>
  40. #include <asm/reboot.h>
  41. /*
  42. * the clockevent devices on Moorestown/Medfield can be APBT or LAPIC clock,
  43. * cmdline option x86_mrst_timer can be used to override the configuration
  44. * to prefer one or the other.
  45. * at runtime, there are basically three timer configurations:
  46. * 1. per cpu apbt clock only
  47. * 2. per cpu always-on lapic clocks only, this is Penwell/Medfield only
  48. * 3. per cpu lapic clock (C3STOP) and one apbt clock, with broadcast.
  49. *
  50. * by default (without cmdline option), platform code first detects cpu type
  51. * to see if we are on lincroft or penwell, then set up both lapic or apbt
  52. * clocks accordingly.
  53. * i.e. by default, medfield uses configuration #2, moorestown uses #1.
  54. * config #3 is supported but not recommended on medfield.
  55. *
  56. * rating and feature summary:
  57. * lapic (with C3STOP) --------- 100
  58. * apbt (always-on) ------------ 110
  59. * lapic (always-on,ARAT) ------ 150
  60. */
  61. __cpuinitdata enum mrst_timer_options mrst_timer_options;
  62. static u32 sfi_mtimer_usage[SFI_MTMR_MAX_NUM];
  63. static struct sfi_timer_table_entry sfi_mtimer_array[SFI_MTMR_MAX_NUM];
  64. enum mrst_cpu_type __mrst_cpu_chip;
  65. EXPORT_SYMBOL_GPL(__mrst_cpu_chip);
  66. int sfi_mtimer_num;
  67. struct sfi_rtc_table_entry sfi_mrtc_array[SFI_MRTC_MAX];
  68. EXPORT_SYMBOL_GPL(sfi_mrtc_array);
  69. int sfi_mrtc_num;
  70. static void mrst_power_off(void)
  71. {
  72. if (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT)
  73. intel_scu_ipc_simple_command(IPCMSG_COLD_RESET, 1);
  74. }
  75. static void mrst_reboot(void)
  76. {
  77. if (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT)
  78. intel_scu_ipc_simple_command(IPCMSG_COLD_RESET, 0);
  79. else
  80. intel_scu_ipc_simple_command(IPCMSG_COLD_BOOT, 0);
  81. }
  82. /* parse all the mtimer info to a static mtimer array */
  83. static int __init sfi_parse_mtmr(struct sfi_table_header *table)
  84. {
  85. struct sfi_table_simple *sb;
  86. struct sfi_timer_table_entry *pentry;
  87. struct mpc_intsrc mp_irq;
  88. int totallen;
  89. sb = (struct sfi_table_simple *)table;
  90. if (!sfi_mtimer_num) {
  91. sfi_mtimer_num = SFI_GET_NUM_ENTRIES(sb,
  92. struct sfi_timer_table_entry);
  93. pentry = (struct sfi_timer_table_entry *) sb->pentry;
  94. totallen = sfi_mtimer_num * sizeof(*pentry);
  95. memcpy(sfi_mtimer_array, pentry, totallen);
  96. }
  97. pr_debug("SFI MTIMER info (num = %d):\n", sfi_mtimer_num);
  98. pentry = sfi_mtimer_array;
  99. for (totallen = 0; totallen < sfi_mtimer_num; totallen++, pentry++) {
  100. pr_debug("timer[%d]: paddr = 0x%08x, freq = %dHz,"
  101. " irq = %d\n", totallen, (u32)pentry->phys_addr,
  102. pentry->freq_hz, pentry->irq);
  103. if (!pentry->irq)
  104. continue;
  105. mp_irq.type = MP_INTSRC;
  106. mp_irq.irqtype = mp_INT;
  107. /* triggering mode edge bit 2-3, active high polarity bit 0-1 */
  108. mp_irq.irqflag = 5;
  109. mp_irq.srcbus = MP_BUS_ISA;
  110. mp_irq.srcbusirq = pentry->irq; /* IRQ */
  111. mp_irq.dstapic = MP_APIC_ALL;
  112. mp_irq.dstirq = pentry->irq;
  113. mp_save_irq(&mp_irq);
  114. }
  115. return 0;
  116. }
  117. struct sfi_timer_table_entry *sfi_get_mtmr(int hint)
  118. {
  119. int i;
  120. if (hint < sfi_mtimer_num) {
  121. if (!sfi_mtimer_usage[hint]) {
  122. pr_debug("hint taken for timer %d irq %d\n",\
  123. hint, sfi_mtimer_array[hint].irq);
  124. sfi_mtimer_usage[hint] = 1;
  125. return &sfi_mtimer_array[hint];
  126. }
  127. }
  128. /* take the first timer available */
  129. for (i = 0; i < sfi_mtimer_num;) {
  130. if (!sfi_mtimer_usage[i]) {
  131. sfi_mtimer_usage[i] = 1;
  132. return &sfi_mtimer_array[i];
  133. }
  134. i++;
  135. }
  136. return NULL;
  137. }
  138. void sfi_free_mtmr(struct sfi_timer_table_entry *mtmr)
  139. {
  140. int i;
  141. for (i = 0; i < sfi_mtimer_num;) {
  142. if (mtmr->irq == sfi_mtimer_array[i].irq) {
  143. sfi_mtimer_usage[i] = 0;
  144. return;
  145. }
  146. i++;
  147. }
  148. }
  149. /* parse all the mrtc info to a global mrtc array */
  150. int __init sfi_parse_mrtc(struct sfi_table_header *table)
  151. {
  152. struct sfi_table_simple *sb;
  153. struct sfi_rtc_table_entry *pentry;
  154. struct mpc_intsrc mp_irq;
  155. int totallen;
  156. sb = (struct sfi_table_simple *)table;
  157. if (!sfi_mrtc_num) {
  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);
  163. }
  164. pr_debug("SFI RTC info (num = %d):\n", sfi_mrtc_num);
  165. pentry = sfi_mrtc_array;
  166. for (totallen = 0; totallen < sfi_mrtc_num; totallen++, pentry++) {
  167. pr_debug("RTC[%d]: paddr = 0x%08x, irq = %d\n",
  168. totallen, (u32)pentry->phys_addr, pentry->irq);
  169. mp_irq.type = MP_INTSRC;
  170. mp_irq.irqtype = mp_INT;
  171. mp_irq.irqflag = 0xf; /* level trigger and active low */
  172. mp_irq.srcbus = MP_BUS_ISA;
  173. mp_irq.srcbusirq = pentry->irq; /* IRQ */
  174. mp_irq.dstapic = MP_APIC_ALL;
  175. mp_irq.dstirq = pentry->irq;
  176. mp_save_irq(&mp_irq);
  177. }
  178. return 0;
  179. }
  180. static unsigned long __init mrst_calibrate_tsc(void)
  181. {
  182. unsigned long flags, fast_calibrate;
  183. if (__mrst_cpu_chip == MRST_CPU_CHIP_PENWELL) {
  184. u32 lo, hi, ratio, fsb;
  185. rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
  186. pr_debug("IA32 perf status is 0x%x, 0x%0x\n", lo, hi);
  187. ratio = (hi >> 8) & 0x1f;
  188. pr_debug("ratio is %d\n", ratio);
  189. if (!ratio) {
  190. pr_err("read a zero ratio, should be incorrect!\n");
  191. pr_err("force tsc ratio to 16 ...\n");
  192. ratio = 16;
  193. }
  194. rdmsr(MSR_FSB_FREQ, lo, hi);
  195. if ((lo & 0x7) == 0x7)
  196. fsb = PENWELL_FSB_FREQ_83SKU;
  197. else
  198. fsb = PENWELL_FSB_FREQ_100SKU;
  199. fast_calibrate = ratio * fsb;
  200. pr_debug("read penwell tsc %lu khz\n", fast_calibrate);
  201. lapic_timer_frequency = fsb * 1000 / HZ;
  202. /* mark tsc clocksource as reliable */
  203. set_cpu_cap(&boot_cpu_data, X86_FEATURE_TSC_RELIABLE);
  204. } else {
  205. local_irq_save(flags);
  206. fast_calibrate = apbt_quick_calibrate();
  207. local_irq_restore(flags);
  208. }
  209. if (fast_calibrate)
  210. return fast_calibrate;
  211. return 0;
  212. }
  213. static void __init mrst_time_init(void)
  214. {
  215. sfi_table_parse(SFI_SIG_MTMR, NULL, NULL, sfi_parse_mtmr);
  216. switch (mrst_timer_options) {
  217. case MRST_TIMER_APBT_ONLY:
  218. break;
  219. case MRST_TIMER_LAPIC_APBT:
  220. x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
  221. x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
  222. break;
  223. default:
  224. if (!boot_cpu_has(X86_FEATURE_ARAT))
  225. break;
  226. x86_init.timers.setup_percpu_clockev = setup_boot_APIC_clock;
  227. x86_cpuinit.setup_percpu_clockev = setup_secondary_APIC_clock;
  228. return;
  229. }
  230. /* we need at least one APB timer */
  231. pre_init_apic_IRQ0();
  232. apbt_time_init();
  233. }
  234. static void __cpuinit mrst_arch_setup(void)
  235. {
  236. if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x27)
  237. __mrst_cpu_chip = MRST_CPU_CHIP_PENWELL;
  238. else if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 0x26)
  239. __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
  240. else {
  241. pr_err("Unknown Moorestown CPU (%d:%d), default to Lincroft\n",
  242. boot_cpu_data.x86, boot_cpu_data.x86_model);
  243. __mrst_cpu_chip = MRST_CPU_CHIP_LINCROFT;
  244. }
  245. pr_debug("Moorestown CPU %s identified\n",
  246. (__mrst_cpu_chip == MRST_CPU_CHIP_LINCROFT) ?
  247. "Lincroft" : "Penwell");
  248. }
  249. /* MID systems don't have i8042 controller */
  250. static int mrst_i8042_detect(void)
  251. {
  252. return 0;
  253. }
  254. /*
  255. * Moorestown does not have external NMI source nor port 0x61 to report
  256. * NMI status. The possible NMI sources are from pmu as a result of NMI
  257. * watchdog or lock debug. Reading io port 0x61 results in 0xff which
  258. * misled NMI handler.
  259. */
  260. static unsigned char mrst_get_nmi_reason(void)
  261. {
  262. return 0;
  263. }
  264. /*
  265. * Moorestown specific x86_init function overrides and early setup
  266. * calls.
  267. */
  268. void __init x86_mrst_early_setup(void)
  269. {
  270. x86_init.resources.probe_roms = x86_init_noop;
  271. x86_init.resources.reserve_resources = x86_init_noop;
  272. x86_init.timers.timer_init = mrst_time_init;
  273. x86_init.timers.setup_percpu_clockev = x86_init_noop;
  274. x86_init.irqs.pre_vector_init = x86_init_noop;
  275. x86_init.oem.arch_setup = mrst_arch_setup;
  276. x86_cpuinit.setup_percpu_clockev = apbt_setup_secondary_clock;
  277. x86_platform.calibrate_tsc = mrst_calibrate_tsc;
  278. x86_platform.i8042_detect = mrst_i8042_detect;
  279. x86_init.timers.wallclock_init = mrst_rtc_init;
  280. x86_platform.get_nmi_reason = mrst_get_nmi_reason;
  281. x86_init.pci.init = pci_mrst_init;
  282. x86_init.pci.fixup_irqs = x86_init_noop;
  283. legacy_pic = &null_legacy_pic;
  284. /* Moorestown specific power_off/restart method */
  285. pm_power_off = mrst_power_off;
  286. machine_ops.emergency_restart = mrst_reboot;
  287. /* Avoid searching for BIOS MP tables */
  288. x86_init.mpparse.find_smp_config = x86_init_noop;
  289. x86_init.mpparse.get_smp_config = x86_init_uint_noop;
  290. set_bit(MP_BUS_ISA, mp_bus_not_pci);
  291. }
  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. */
  296. static inline int __init setup_x86_mrst_timer(char *arg)
  297. {
  298. if (!arg)
  299. return -EINVAL;
  300. if (strcmp("apbt_only", arg) == 0)
  301. mrst_timer_options = MRST_TIMER_APBT_ONLY;
  302. else if (strcmp("lapic_and_apbt", arg) == 0)
  303. mrst_timer_options = MRST_TIMER_LAPIC_APBT;
  304. else {
  305. pr_warning("X86 MRST timer option %s not recognised"
  306. " use x86_mrst_timer=apbt_only or lapic_and_apbt\n",
  307. arg);
  308. return -EINVAL;
  309. }
  310. return 0;
  311. }
  312. __setup("x86_mrst_timer=", setup_x86_mrst_timer);
  313. /*
  314. * Parsing GPIO table first, since the DEVS table will need this table
  315. * to map the pin name to the actual pin.
  316. */
  317. static struct sfi_gpio_table_entry *gpio_table;
  318. static int gpio_num_entry;
  319. static int __init sfi_parse_gpio(struct sfi_table_header *table)
  320. {
  321. struct sfi_table_simple *sb;
  322. struct sfi_gpio_table_entry *pentry;
  323. int num, i;
  324. if (gpio_table)
  325. return 0;
  326. sb = (struct sfi_table_simple *)table;
  327. num = SFI_GET_NUM_ENTRIES(sb, struct sfi_gpio_table_entry);
  328. pentry = (struct sfi_gpio_table_entry *)sb->pentry;
  329. gpio_table = (struct sfi_gpio_table_entry *)
  330. kmalloc(num * sizeof(*pentry), GFP_KERNEL);
  331. if (!gpio_table)
  332. return -1;
  333. memcpy(gpio_table, pentry, num * sizeof(*pentry));
  334. gpio_num_entry = num;
  335. pr_debug("GPIO pin info:\n");
  336. for (i = 0; i < num; i++, pentry++)
  337. pr_debug("info[%2d]: controller = %16.16s, pin_name = %16.16s,"
  338. " pin = %d\n", i,
  339. pentry->controller_name,
  340. pentry->pin_name,
  341. pentry->pin_no);
  342. return 0;
  343. }
  344. static int get_gpio_by_name(const char *name)
  345. {
  346. struct sfi_gpio_table_entry *pentry = gpio_table;
  347. int i;
  348. if (!pentry)
  349. return -1;
  350. for (i = 0; i < gpio_num_entry; i++, pentry++) {
  351. if (!strncmp(name, pentry->pin_name, SFI_NAME_LEN))
  352. return pentry->pin_no;
  353. }
  354. return -1;
  355. }
  356. /*
  357. * Here defines the array of devices platform data that IAFW would export
  358. * through SFI "DEVS" table, we use name and type to match the device and
  359. * its platform data.
  360. */
  361. struct devs_id {
  362. char name[SFI_NAME_LEN + 1];
  363. u8 type;
  364. u8 delay;
  365. void *(*get_platform_data)(void *info);
  366. };
  367. /* the offset for the mapping of global gpio pin to irq */
  368. #define MRST_IRQ_OFFSET 0x100
  369. static void __init *pmic_gpio_platform_data(void *info)
  370. {
  371. static struct intel_pmic_gpio_platform_data pmic_gpio_pdata;
  372. int gpio_base = get_gpio_by_name("pmic_gpio_base");
  373. if (gpio_base == -1)
  374. gpio_base = 64;
  375. pmic_gpio_pdata.gpio_base = gpio_base;
  376. pmic_gpio_pdata.irq_base = gpio_base + MRST_IRQ_OFFSET;
  377. pmic_gpio_pdata.gpiointr = 0xffffeff8;
  378. return &pmic_gpio_pdata;
  379. }
  380. static void __init *max3111_platform_data(void *info)
  381. {
  382. struct spi_board_info *spi_info = info;
  383. int intr = get_gpio_by_name("max3111_int");
  384. spi_info->mode = SPI_MODE_0;
  385. if (intr == -1)
  386. return NULL;
  387. spi_info->irq = intr + MRST_IRQ_OFFSET;
  388. return NULL;
  389. }
  390. /* we have multiple max7315 on the board ... */
  391. #define MAX7315_NUM 2
  392. static void __init *max7315_platform_data(void *info)
  393. {
  394. static struct pca953x_platform_data max7315_pdata[MAX7315_NUM];
  395. static int nr;
  396. struct pca953x_platform_data *max7315 = &max7315_pdata[nr];
  397. struct i2c_board_info *i2c_info = info;
  398. int gpio_base, intr;
  399. char base_pin_name[SFI_NAME_LEN + 1];
  400. char intr_pin_name[SFI_NAME_LEN + 1];
  401. if (nr == MAX7315_NUM) {
  402. pr_err("too many max7315s, we only support %d\n",
  403. MAX7315_NUM);
  404. return NULL;
  405. }
  406. /* we have several max7315 on the board, we only need load several
  407. * instances of the same pca953x driver to cover them
  408. */
  409. strcpy(i2c_info->type, "max7315");
  410. if (nr++) {
  411. sprintf(base_pin_name, "max7315_%d_base", nr);
  412. sprintf(intr_pin_name, "max7315_%d_int", nr);
  413. } else {
  414. strcpy(base_pin_name, "max7315_base");
  415. strcpy(intr_pin_name, "max7315_int");
  416. }
  417. gpio_base = get_gpio_by_name(base_pin_name);
  418. intr = get_gpio_by_name(intr_pin_name);
  419. if (gpio_base == -1)
  420. return NULL;
  421. max7315->gpio_base = gpio_base;
  422. if (intr != -1) {
  423. i2c_info->irq = intr + MRST_IRQ_OFFSET;
  424. max7315->irq_base = gpio_base + MRST_IRQ_OFFSET;
  425. } else {
  426. i2c_info->irq = -1;
  427. max7315->irq_base = -1;
  428. }
  429. return max7315;
  430. }
  431. static void __init *emc1403_platform_data(void *info)
  432. {
  433. static short intr2nd_pdata;
  434. struct i2c_board_info *i2c_info = info;
  435. int intr = get_gpio_by_name("thermal_int");
  436. int intr2nd = get_gpio_by_name("thermal_alert");
  437. if (intr == -1 || intr2nd == -1)
  438. return NULL;
  439. i2c_info->irq = intr + MRST_IRQ_OFFSET;
  440. intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
  441. return &intr2nd_pdata;
  442. }
  443. static void __init *lis331dl_platform_data(void *info)
  444. {
  445. static short intr2nd_pdata;
  446. struct i2c_board_info *i2c_info = info;
  447. int intr = get_gpio_by_name("accel_int");
  448. int intr2nd = get_gpio_by_name("accel_2");
  449. if (intr == -1 || intr2nd == -1)
  450. return NULL;
  451. i2c_info->irq = intr + MRST_IRQ_OFFSET;
  452. intr2nd_pdata = intr2nd + MRST_IRQ_OFFSET;
  453. return &intr2nd_pdata;
  454. }
  455. static void __init *no_platform_data(void *info)
  456. {
  457. return NULL;
  458. }
  459. static struct resource msic_resources[] = {
  460. {
  461. .start = INTEL_MSIC_IRQ_PHYS_BASE,
  462. .end = INTEL_MSIC_IRQ_PHYS_BASE + 64 - 1,
  463. .flags = IORESOURCE_MEM,
  464. },
  465. };
  466. static struct intel_msic_platform_data msic_pdata;
  467. static struct platform_device msic_device = {
  468. .name = "intel_msic",
  469. .id = -1,
  470. .dev = {
  471. .platform_data = &msic_pdata,
  472. },
  473. .num_resources = ARRAY_SIZE(msic_resources),
  474. .resource = msic_resources,
  475. };
  476. static inline bool mrst_has_msic(void)
  477. {
  478. return mrst_identify_cpu() == MRST_CPU_CHIP_PENWELL;
  479. }
  480. static int msic_scu_status_change(struct notifier_block *nb,
  481. unsigned long code, void *data)
  482. {
  483. if (code == SCU_DOWN) {
  484. platform_device_unregister(&msic_device);
  485. return 0;
  486. }
  487. return platform_device_register(&msic_device);
  488. }
  489. static int __init msic_init(void)
  490. {
  491. static struct notifier_block msic_scu_notifier = {
  492. .notifier_call = msic_scu_status_change,
  493. };
  494. /*
  495. * We need to be sure that the SCU IPC is ready before MSIC device
  496. * can be registered.
  497. */
  498. if (mrst_has_msic())
  499. intel_scu_notifier_add(&msic_scu_notifier);
  500. return 0;
  501. }
  502. arch_initcall(msic_init);
  503. /*
  504. * msic_generic_platform_data - sets generic platform data for the block
  505. * @info: pointer to the SFI device table entry for this block
  506. * @block: MSIC block
  507. *
  508. * Function sets IRQ number from the SFI table entry for given device to
  509. * the MSIC platform data.
  510. */
  511. static void *msic_generic_platform_data(void *info, enum intel_msic_block block)
  512. {
  513. struct sfi_device_table_entry *entry = info;
  514. BUG_ON(block < 0 || block >= INTEL_MSIC_BLOCK_LAST);
  515. msic_pdata.irq[block] = entry->irq;
  516. return no_platform_data(info);
  517. }
  518. static void *msic_battery_platform_data(void *info)
  519. {
  520. return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_BATTERY);
  521. }
  522. static void *msic_gpio_platform_data(void *info)
  523. {
  524. static struct intel_msic_gpio_pdata pdata;
  525. int gpio = get_gpio_by_name("msic_gpio_base");
  526. if (gpio < 0)
  527. return NULL;
  528. pdata.gpio_base = gpio;
  529. msic_pdata.gpio = &pdata;
  530. return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_GPIO);
  531. }
  532. static void *msic_audio_platform_data(void *info)
  533. {
  534. struct platform_device *pdev;
  535. pdev = platform_device_register_simple("sst-platform", -1, NULL, 0);
  536. if (IS_ERR(pdev)) {
  537. pr_err("failed to create audio platform device\n");
  538. return NULL;
  539. }
  540. return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_AUDIO);
  541. }
  542. static void *msic_power_btn_platform_data(void *info)
  543. {
  544. return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_POWER_BTN);
  545. }
  546. static void *msic_ocd_platform_data(void *info)
  547. {
  548. static struct intel_msic_ocd_pdata pdata;
  549. int gpio = get_gpio_by_name("ocd_gpio");
  550. if (gpio < 0)
  551. return NULL;
  552. pdata.gpio = gpio;
  553. msic_pdata.ocd = &pdata;
  554. return msic_generic_platform_data(info, INTEL_MSIC_BLOCK_OCD);
  555. }
  556. static const struct devs_id __initconst device_ids[] = {
  557. {"bma023", SFI_DEV_TYPE_I2C, 1, &no_platform_data},
  558. {"pmic_gpio", SFI_DEV_TYPE_SPI, 1, &pmic_gpio_platform_data},
  559. {"spi_max3111", SFI_DEV_TYPE_SPI, 0, &max3111_platform_data},
  560. {"i2c_max7315", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
  561. {"i2c_max7315_2", SFI_DEV_TYPE_I2C, 1, &max7315_platform_data},
  562. {"emc1403", SFI_DEV_TYPE_I2C, 1, &emc1403_platform_data},
  563. {"i2c_accel", SFI_DEV_TYPE_I2C, 0, &lis331dl_platform_data},
  564. {"pmic_audio", SFI_DEV_TYPE_IPC, 1, &no_platform_data},
  565. /* MSIC subdevices */
  566. {"msic_battery", SFI_DEV_TYPE_IPC, 1, &msic_battery_platform_data},
  567. {"msic_gpio", SFI_DEV_TYPE_IPC, 1, &msic_gpio_platform_data},
  568. {"msic_audio", SFI_DEV_TYPE_IPC, 1, &msic_audio_platform_data},
  569. {"msic_power_btn", SFI_DEV_TYPE_IPC, 1, &msic_power_btn_platform_data},
  570. {"msic_ocd", SFI_DEV_TYPE_IPC, 1, &msic_ocd_platform_data},
  571. {},
  572. };
  573. #define MAX_IPCDEVS 24
  574. static struct platform_device *ipc_devs[MAX_IPCDEVS];
  575. static int ipc_next_dev;
  576. #define MAX_SCU_SPI 24
  577. static struct spi_board_info *spi_devs[MAX_SCU_SPI];
  578. static int spi_next_dev;
  579. #define MAX_SCU_I2C 24
  580. static struct i2c_board_info *i2c_devs[MAX_SCU_I2C];
  581. static int i2c_bus[MAX_SCU_I2C];
  582. static int i2c_next_dev;
  583. static void __init intel_scu_device_register(struct platform_device *pdev)
  584. {
  585. if(ipc_next_dev == MAX_IPCDEVS)
  586. pr_err("too many SCU IPC devices");
  587. else
  588. ipc_devs[ipc_next_dev++] = pdev;
  589. }
  590. static void __init intel_scu_spi_device_register(struct spi_board_info *sdev)
  591. {
  592. struct spi_board_info *new_dev;
  593. if (spi_next_dev == MAX_SCU_SPI) {
  594. pr_err("too many SCU SPI devices");
  595. return;
  596. }
  597. new_dev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  598. if (!new_dev) {
  599. pr_err("failed to alloc mem for delayed spi dev %s\n",
  600. sdev->modalias);
  601. return;
  602. }
  603. memcpy(new_dev, sdev, sizeof(*sdev));
  604. spi_devs[spi_next_dev++] = new_dev;
  605. }
  606. static void __init intel_scu_i2c_device_register(int bus,
  607. struct i2c_board_info *idev)
  608. {
  609. struct i2c_board_info *new_dev;
  610. if (i2c_next_dev == MAX_SCU_I2C) {
  611. pr_err("too many SCU I2C devices");
  612. return;
  613. }
  614. new_dev = kzalloc(sizeof(*idev), GFP_KERNEL);
  615. if (!new_dev) {
  616. pr_err("failed to alloc mem for delayed i2c dev %s\n",
  617. idev->type);
  618. return;
  619. }
  620. memcpy(new_dev, idev, sizeof(*idev));
  621. i2c_bus[i2c_next_dev] = bus;
  622. i2c_devs[i2c_next_dev++] = new_dev;
  623. }
  624. BLOCKING_NOTIFIER_HEAD(intel_scu_notifier);
  625. EXPORT_SYMBOL_GPL(intel_scu_notifier);
  626. /* Called by IPC driver */
  627. void intel_scu_devices_create(void)
  628. {
  629. int i;
  630. for (i = 0; i < ipc_next_dev; i++)
  631. platform_device_add(ipc_devs[i]);
  632. for (i = 0; i < spi_next_dev; i++)
  633. spi_register_board_info(spi_devs[i], 1);
  634. for (i = 0; i < i2c_next_dev; i++) {
  635. struct i2c_adapter *adapter;
  636. struct i2c_client *client;
  637. adapter = i2c_get_adapter(i2c_bus[i]);
  638. if (adapter) {
  639. client = i2c_new_device(adapter, i2c_devs[i]);
  640. if (!client)
  641. pr_err("can't create i2c device %s\n",
  642. i2c_devs[i]->type);
  643. } else
  644. i2c_register_board_info(i2c_bus[i], i2c_devs[i], 1);
  645. }
  646. intel_scu_notifier_post(SCU_AVAILABLE, 0L);
  647. }
  648. EXPORT_SYMBOL_GPL(intel_scu_devices_create);
  649. /* Called by IPC driver */
  650. void intel_scu_devices_destroy(void)
  651. {
  652. int i;
  653. intel_scu_notifier_post(SCU_DOWN, 0L);
  654. for (i = 0; i < ipc_next_dev; i++)
  655. platform_device_del(ipc_devs[i]);
  656. }
  657. EXPORT_SYMBOL_GPL(intel_scu_devices_destroy);
  658. static void __init install_irq_resource(struct platform_device *pdev, int irq)
  659. {
  660. /* Single threaded */
  661. static struct resource __initdata res = {
  662. .name = "IRQ",
  663. .flags = IORESOURCE_IRQ,
  664. };
  665. res.start = irq;
  666. platform_device_add_resources(pdev, &res, 1);
  667. }
  668. static void __init sfi_handle_ipc_dev(struct sfi_device_table_entry *entry)
  669. {
  670. const struct devs_id *dev = device_ids;
  671. struct platform_device *pdev;
  672. void *pdata = NULL;
  673. while (dev->name[0]) {
  674. if (dev->type == SFI_DEV_TYPE_IPC &&
  675. !strncmp(dev->name, entry->name, SFI_NAME_LEN)) {
  676. pdata = dev->get_platform_data(entry);
  677. break;
  678. }
  679. dev++;
  680. }
  681. /*
  682. * On Medfield the platform device creation is handled by the MSIC
  683. * MFD driver so we don't need to do it here.
  684. */
  685. if (mrst_has_msic())
  686. return;
  687. /* ID as IRQ is a hack that will go away */
  688. pdev = platform_device_alloc(entry->name, entry->irq);
  689. if (pdev == NULL) {
  690. pr_err("out of memory for SFI platform device '%s'.\n",
  691. entry->name);
  692. return;
  693. }
  694. install_irq_resource(pdev, entry->irq);
  695. pdev->dev.platform_data = pdata;
  696. intel_scu_device_register(pdev);
  697. }
  698. static void __init sfi_handle_spi_dev(struct spi_board_info *spi_info)
  699. {
  700. const struct devs_id *dev = device_ids;
  701. void *pdata = NULL;
  702. while (dev->name[0]) {
  703. if (dev->type == SFI_DEV_TYPE_SPI &&
  704. !strncmp(dev->name, spi_info->modalias, SFI_NAME_LEN)) {
  705. pdata = dev->get_platform_data(spi_info);
  706. break;
  707. }
  708. dev++;
  709. }
  710. spi_info->platform_data = pdata;
  711. if (dev->delay)
  712. intel_scu_spi_device_register(spi_info);
  713. else
  714. spi_register_board_info(spi_info, 1);
  715. }
  716. static void __init sfi_handle_i2c_dev(int bus, struct i2c_board_info *i2c_info)
  717. {
  718. const struct devs_id *dev = device_ids;
  719. void *pdata = NULL;
  720. while (dev->name[0]) {
  721. if (dev->type == SFI_DEV_TYPE_I2C &&
  722. !strncmp(dev->name, i2c_info->type, SFI_NAME_LEN)) {
  723. pdata = dev->get_platform_data(i2c_info);
  724. break;
  725. }
  726. dev++;
  727. }
  728. i2c_info->platform_data = pdata;
  729. if (dev->delay)
  730. intel_scu_i2c_device_register(bus, i2c_info);
  731. else
  732. i2c_register_board_info(bus, i2c_info, 1);
  733. }
  734. static int __init sfi_parse_devs(struct sfi_table_header *table)
  735. {
  736. struct sfi_table_simple *sb;
  737. struct sfi_device_table_entry *pentry;
  738. struct spi_board_info spi_info;
  739. struct i2c_board_info i2c_info;
  740. int num, i, bus;
  741. int ioapic;
  742. struct io_apic_irq_attr irq_attr;
  743. sb = (struct sfi_table_simple *)table;
  744. num = SFI_GET_NUM_ENTRIES(sb, struct sfi_device_table_entry);
  745. pentry = (struct sfi_device_table_entry *)sb->pentry;
  746. for (i = 0; i < num; i++, pentry++) {
  747. int irq = pentry->irq;
  748. if (irq != (u8)0xff) { /* native RTE case */
  749. /* these SPI2 devices are not exposed to system as PCI
  750. * devices, but they have separate RTE entry in IOAPIC
  751. * so we have to enable them one by one here
  752. */
  753. ioapic = mp_find_ioapic(irq);
  754. irq_attr.ioapic = ioapic;
  755. irq_attr.ioapic_pin = irq;
  756. irq_attr.trigger = 1;
  757. irq_attr.polarity = 1;
  758. io_apic_set_pci_routing(NULL, irq, &irq_attr);
  759. } else
  760. irq = 0; /* No irq */
  761. switch (pentry->type) {
  762. case SFI_DEV_TYPE_IPC:
  763. pr_debug("info[%2d]: IPC bus, name = %16.16s, "
  764. "irq = 0x%2x\n", i, pentry->name, pentry->irq);
  765. sfi_handle_ipc_dev(pentry);
  766. break;
  767. case SFI_DEV_TYPE_SPI:
  768. memset(&spi_info, 0, sizeof(spi_info));
  769. strncpy(spi_info.modalias, pentry->name, SFI_NAME_LEN);
  770. spi_info.irq = irq;
  771. spi_info.bus_num = pentry->host_num;
  772. spi_info.chip_select = pentry->addr;
  773. spi_info.max_speed_hz = pentry->max_freq;
  774. pr_debug("info[%2d]: SPI bus = %d, name = %16.16s, "
  775. "irq = 0x%2x, max_freq = %d, cs = %d\n", i,
  776. spi_info.bus_num,
  777. spi_info.modalias,
  778. spi_info.irq,
  779. spi_info.max_speed_hz,
  780. spi_info.chip_select);
  781. sfi_handle_spi_dev(&spi_info);
  782. break;
  783. case SFI_DEV_TYPE_I2C:
  784. memset(&i2c_info, 0, sizeof(i2c_info));
  785. bus = pentry->host_num;
  786. strncpy(i2c_info.type, pentry->name, SFI_NAME_LEN);
  787. i2c_info.irq = irq;
  788. i2c_info.addr = pentry->addr;
  789. pr_debug("info[%2d]: I2C bus = %d, name = %16.16s, "
  790. "irq = 0x%2x, addr = 0x%x\n", i, bus,
  791. i2c_info.type,
  792. i2c_info.irq,
  793. i2c_info.addr);
  794. sfi_handle_i2c_dev(bus, &i2c_info);
  795. break;
  796. case SFI_DEV_TYPE_UART:
  797. case SFI_DEV_TYPE_HSI:
  798. default:
  799. ;
  800. }
  801. }
  802. return 0;
  803. }
  804. static int __init mrst_platform_init(void)
  805. {
  806. sfi_table_parse(SFI_SIG_GPIO, NULL, NULL, sfi_parse_gpio);
  807. sfi_table_parse(SFI_SIG_DEVS, NULL, NULL, sfi_parse_devs);
  808. return 0;
  809. }
  810. arch_initcall(mrst_platform_init);
  811. /*
  812. * we will search these buttons in SFI GPIO table (by name)
  813. * and register them dynamically. Please add all possible
  814. * buttons here, we will shrink them if no GPIO found.
  815. */
  816. static struct gpio_keys_button gpio_button[] = {
  817. {KEY_POWER, -1, 1, "power_btn", EV_KEY, 0, 3000},
  818. {KEY_PROG1, -1, 1, "prog_btn1", EV_KEY, 0, 20},
  819. {KEY_PROG2, -1, 1, "prog_btn2", EV_KEY, 0, 20},
  820. {SW_LID, -1, 1, "lid_switch", EV_SW, 0, 20},
  821. {KEY_VOLUMEUP, -1, 1, "vol_up", EV_KEY, 0, 20},
  822. {KEY_VOLUMEDOWN, -1, 1, "vol_down", EV_KEY, 0, 20},
  823. {KEY_CAMERA, -1, 1, "camera_full", EV_KEY, 0, 20},
  824. {KEY_CAMERA_FOCUS, -1, 1, "camera_half", EV_KEY, 0, 20},
  825. {SW_KEYPAD_SLIDE, -1, 1, "MagSw1", EV_SW, 0, 20},
  826. {SW_KEYPAD_SLIDE, -1, 1, "MagSw2", EV_SW, 0, 20},
  827. };
  828. static struct gpio_keys_platform_data mrst_gpio_keys = {
  829. .buttons = gpio_button,
  830. .rep = 1,
  831. .nbuttons = -1, /* will fill it after search */
  832. };
  833. static struct platform_device pb_device = {
  834. .name = "gpio-keys",
  835. .id = -1,
  836. .dev = {
  837. .platform_data = &mrst_gpio_keys,
  838. },
  839. };
  840. /*
  841. * Shrink the non-existent buttons, register the gpio button
  842. * device if there is some
  843. */
  844. static int __init pb_keys_init(void)
  845. {
  846. struct gpio_keys_button *gb = gpio_button;
  847. int i, num, good = 0;
  848. num = sizeof(gpio_button) / sizeof(struct gpio_keys_button);
  849. for (i = 0; i < num; i++) {
  850. gb[i].gpio = get_gpio_by_name(gb[i].desc);
  851. if (gb[i].gpio == -1)
  852. continue;
  853. if (i != good)
  854. gb[good] = gb[i];
  855. good++;
  856. }
  857. if (good) {
  858. mrst_gpio_keys.nbuttons = good;
  859. return platform_device_register(&pb_device);
  860. }
  861. return 0;
  862. }
  863. late_initcall(pb_keys_init);