summaryrefslogtreecommitdiff
path: root/target/linux/olpc/files/arch/i386/kernel/olpc-pm.c
blob: 93149f1c38c67446dcd52505b93194e08ac770a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/* olpc-pm.c
 * © 2006 Red Hat, Inc.
 * Portions also copyright 2006 Advanced Micro Devices, Inc.
 * GPLv2
 */

#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/suspend.h>
#include <linux/bootmem.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/mc146818rtc.h>
#include <asm/io.h>

#include <asm/olpc.h>

/* A few words about accessing the ACPI and PM registers.  Long story short,
   byte and word accesses of the ACPI and PM registers is broken.  The only
   way to do it really correctly is to use dword accesses, which we do
   throughout this code.  For more details, please consult Eratta 17 and 18
   here:

   http://www.amd.com/files/connectivitysolutions/geode/geode_gx/34472D_CS5536_B1_specupdate.pdf
*/

#define PM_IRQ 3

#define CS5536_PM_PWRBTN (1 << 8)
#define CS5536_PM_RTC    (1 << 10)

#define GPIO_WAKEUP_EC (1 << 31)
#define GPIO_WAKEUP_LID (1 << 30)

#define PM_MODE_NORMAL 0
#define PM_MODE_TEST   1
#define PM_MODE_MAX    2

/* These, and the battery EC commands, should be in an olpc.h. */
#define EC_WRITE_SCI_MASK 0x1b
#define EC_READ_SCI_MASK  0x1c

extern void do_olpc_suspend_lowlevel(void);

static struct {
	unsigned long address;
	unsigned short segment;
} ofw_bios_entry = { 0, __KERNEL_CS };

static int olpc_pm_mode = PM_MODE_NORMAL;
static unsigned long acpi_base;
static unsigned long pms_base;
static int sci_irq;
static int olpc_lid_flag;

static struct input_dev *pm_inputdev;
static struct input_dev *lid_inputdev;
static struct input_dev *ebook_inputdev;
static struct pm_ops olpc_pm_ops;

static int gpio_wake_events = 0;
static int ebook_state = -1;
static u16 olpc_wakeup_mask = 0;

struct platform_device olpc_powerbutton_dev = {
	.name = "powerbutton",
	.id = -1,
};

struct platform_device olpc_lid_dev = {
	.name = "lid",
	.id = -1,
};

static void __init init_ebook_state(void)
{
	if (olpc_ec_cmd(0x2a, NULL, 0, (unsigned char *) &ebook_state, 1)) {
		printk(KERN_WARNING "olpc-pm:  failed to get EBOOK state!\n");
		ebook_state = 0;
	}
	ebook_state &= 1;

	/* the input layer needs to know what value to default to as well */
	input_report_switch(ebook_inputdev, SW_TABLET_MODE, ebook_state);
	input_sync(ebook_inputdev);
}

static void (*battery_callback)(unsigned long);
static DEFINE_SPINLOCK(battery_callback_lock);

/* propagate_events is non-NULL if run from workqueue,
   NULL when called at init time to flush SCI queue */
static void process_sci_queue(struct work_struct *propagate_events)
{
	unsigned char data = 0;
	unsigned char battery_events = 0;
	int ret;

	do {
		ret = olpc_ec_cmd(0x84, NULL, 0, &data, 1);
		if (!ret) {
			printk(KERN_DEBUG "olpc-pm:  SCI 0x%x received\n",
					data);

			switch (data) {
			case EC_SCI_SRC_EMPTY:
			case EC_SCI_SRC_GAME:
			case EC_SCI_SRC_WLAN:
				/* we ignore these for now */
				break;
			case EC_SCI_SRC_BATERR:
				printk(KERN_ERR "olpc-pm:  Battery Management System detected an error!  Remove turnip from battery slot.\n");
			case EC_SCI_SRC_BATSOC:
			case EC_SCI_SRC_BATTERY:
			case EC_SCI_SRC_ACPWR:
				battery_events |= data;
				break;
			case EC_SCI_SRC_EBOOK:
				ebook_state = !ebook_state;
				if (propagate_events) {
					input_report_switch(ebook_inputdev,
						SW_TABLET_MODE, ebook_state);
					input_sync(ebook_inputdev);
				}
				break;
			default:
				printk(KERN_ERR "olpc-pm:  Unknown SCI event 0x%x occurred!\n", data);
			}
		}
	} while (data && !ret);

	if (battery_events && battery_callback && propagate_events) {
		void (*cbk)(unsigned long);
		
		/* Older EC versions didn't distinguish between AC and battery
		   events */
		if (olpc_platform_info.ecver < 0x45)
			battery_events = EC_SCI_SRC_BATTERY | EC_SCI_SRC_ACPWR;

		spin_lock(&battery_callback_lock);
		cbk = battery_callback;
		spin_unlock(&battery_callback_lock);

		cbk(battery_events);
	}

	if (ret)
		printk(KERN_WARNING "Failed to clear SCI queue!\n");
}

static DECLARE_WORK(sci_work, process_sci_queue);

void olpc_register_battery_callback(void (*f)(unsigned long))
{
	spin_lock(&battery_callback_lock);
	battery_callback = f;
	spin_unlock(&battery_callback_lock);
}
EXPORT_SYMBOL_GPL(olpc_register_battery_callback);

void olpc_deregister_battery_callback(void)
{
	spin_lock(&battery_callback_lock);
	battery_callback = NULL;
	spin_unlock(&battery_callback_lock);
	cancel_work_sync(&sci_work);
}
EXPORT_SYMBOL_GPL(olpc_deregister_battery_callback);


static int olpc_pm_interrupt(int irq, void *id)
{
	uint32_t sts, gpe = 0;

	sts = inl(acpi_base + PM1_STS);
	outl(sts | 0xFFFF, acpi_base + PM1_STS);

	if (olpc_get_rev() >= OLPC_REV_B2) {
		gpe = inl(acpi_base + PM_GPE0_STS);
		outl(0xFFFFFFFF, acpi_base + PM_GPE0_STS);
	}

	if (sts & CS5536_PM_PWRBTN) {
		input_report_key(pm_inputdev, KEY_POWER, 1);
		input_sync(pm_inputdev);
		printk(KERN_DEBUG "olpm-pm:  PM_PWRBTN event received\n");
		/* Do we need to delay this (and hence schedule_work)? */
		input_report_key(pm_inputdev, KEY_POWER, 0);
		input_sync(pm_inputdev);
	}

	if (gpe & GPIO_WAKEUP_EC) {
		geode_gpio_clear(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
		schedule_work(&sci_work);
	}

	if (gpe & GPIO_WAKEUP_LID) {
		/* Disable events */
		geode_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
			
		/* Clear the edge */
			
		if (olpc_lid_flag)
			geode_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
		else
			geode_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);

		/* Clear the status too */
		geode_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
		geode_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);

		/* The line is high when the LID is open, but SW_LID
		 * should be high when the LID is closed, so we pass the old
		 * value of olpc_lid_flag
		 */

		input_report_switch(lid_inputdev, SW_LID, olpc_lid_flag);
		input_sync(lid_inputdev);

		/* Swap the status */
		olpc_lid_flag = !olpc_lid_flag;

		if (olpc_lid_flag)
			geode_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
		else
			geode_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);

		/* re-enable the event */
		geode_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
	}

	return IRQ_HANDLED;
}

/*
 * For now, only support STR.  We also don't support suspending on
 * B1s, due to difficulties with the cafe FPGA.
 */
static int olpc_pm_state_valid(suspend_state_t pm_state)
{
        if (pm_state == PM_SUSPEND_MEM && olpc_rev_after(OLPC_REV_B1))
                return 1;

        return 0;
}

/* This is a catchall function for operations that just don't belong
 * anywhere else.  Later we will evaluate if these belong in the
 * individual device drivers or the firmware.
 * If you add something to this function, please explain yourself with
 * a comment.
 */

extern void gxfb_flatpanel_control(int state);

static u32 gpio_wakeup[2];
static u64 irq_sources[4];
static u64 mfgpt_irq_msr, mfgpt_nr_msr;

void olpc_fixup_wakeup(void)
{
	u32 base = geode_gpio_base();
	int i;

	/* This clears any pending events from the status register -
	 * the firmware also does this, but its possible that it tries
	 * it too early before the key has a chance to debounce
	 */

	outl((CS5536_PM_PWRBTN << 16) | 0xFFFF, acpi_base + PM1_STS);

	/* Enable the flatpanel sequencing as early as possible, because
	   it takes ~64ms to resume.  This probably belongs in the firmware */

	//gxfb_flatpanel_control(1);

	/* Restore the interrupt sources */
	wrmsrl(MSR_PIC_YSEL_LOW, irq_sources[0]);
	wrmsrl(MSR_PIC_ZSEL_LOW, irq_sources[1]);
	wrmsrl(MSR_PIC_YSEL_HIGH, irq_sources[2]);
	wrmsrl(MSR_PIC_ZSEL_HIGH, irq_sources[3]);

	/* Restore the X and Y sources for GPIO */
	outl(gpio_wakeup[0], base + GPIO_MAP_X);
	outl(gpio_wakeup[1], base + GPIO_MAP_Y);

	/* Resture the MFGPT MSRs */
	wrmsrl(MFGPT_IRQ_MSR, mfgpt_irq_msr);
	wrmsrl(MFGPT_NR_MSR, mfgpt_nr_msr);
	
	for (i=0;i<2;i++) {
		/* tell the wireless module to restart USB communication */
		olpc_ec_cmd(0x24, NULL, 0, NULL, 0);
	}
}

void olpc_fixup_sleep(void)
{
	u32 base = geode_gpio_base();
	int i;

	/* Save the X and Y sources for GPIO */
	gpio_wakeup[0] = inl(base + GPIO_MAP_X);
	gpio_wakeup[1] = inl(base + GPIO_MAP_Y);

	/* Save the Y and Z  unrestricted sources */

	rdmsrl(MSR_PIC_YSEL_LOW, irq_sources[0]);
	rdmsrl(MSR_PIC_ZSEL_LOW, irq_sources[1]);
	rdmsrl(MSR_PIC_YSEL_HIGH, irq_sources[2]);
	rdmsrl(MSR_PIC_ZSEL_HIGH, irq_sources[3]);

	/* Turn off the MFGPT timers on the way down */

	for(i = 0; i < 8; i++) {
		u32 val = geode_mfgpt_read(i, MFGPT_REG_SETUP);

		if (val & MFGPT_SETUP_SETUP) {
			val &= ~MFGPT_SETUP_CNTEN;
			geode_mfgpt_write(i, MFGPT_REG_SETUP, val);
		}
	}

	/* Save the MFGPT MSRs */
	rdmsrl(MFGPT_IRQ_MSR, mfgpt_irq_msr);
	rdmsrl(MFGPT_NR_MSR, mfgpt_nr_msr);

	if (device_may_wakeup(&olpc_powerbutton_dev.dev))
		olpc_wakeup_mask |= CS5536_PM_PWRBTN;
	else
		olpc_wakeup_mask &= ~(CS5536_PM_PWRBTN);

	if (device_may_wakeup(&olpc_lid_dev.dev)) {
		geode_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
		gpio_wake_events |= GPIO_WAKEUP_LID;
	} else {
		geode_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
		gpio_wake_events &= ~(GPIO_WAKEUP_LID);
	}
}

static int olpc_pm_enter(suspend_state_t pm_state)
{
        /* Only STR is supported */
        if (pm_state != PM_SUSPEND_MEM)
                return -EINVAL;

	olpc_fixup_sleep();

	/* Set the GPIO wakeup bits */
	outl(gpio_wake_events, acpi_base + PM_GPE0_EN);
	outl(0xFFFFFFFF, acpi_base + PM_GPE0_STS);

        /* Save CPU state */
        do_olpc_suspend_lowlevel();

	olpc_fixup_wakeup();

	/* Restore the SCI wakeup events */
	outl(gpio_wake_events, acpi_base + PM_GPE0_EN);

	return 0;
}

int asmlinkage olpc_do_sleep(u8 sleep_state)
{
	void *pgd_addr = __va(read_cr3());
	printk(KERN_ERR "olpc_do_sleep!\n"); /* this needs to remain here so
					      * that gcc doesn't optimize
					      * away our __va! */
	/* FIXME: Set the SCI bits we want to wake up on here */

	/* FIXME:  Set any other SCI events that we might want here */

	outl((olpc_wakeup_mask << 16) | 0xFFFF, acpi_base + PM1_STS);

	/* If we are in test mode, then just return (simulate a successful
	   suspend/resume).  Otherwise, if we are doing the real thing,
	   then go for the gusto */

 	if (olpc_pm_mode != PM_MODE_TEST) {
		__asm__ __volatile__("movl %0,%%eax" : : "r" (pgd_addr));
		__asm__("call *(%%edi); cld"
			: : "D" (&ofw_bios_entry));
	}

	return 0;
}

/* This code will slowly disappear as we fixup the issues in the BIOS */

static void __init olpc_fixup_bios(void)
{
	unsigned long hi, lo;

	if (olpc_has_vsa()) {
		/* The VSA aggressively sets up the ACPI and PM register for
		 * trapping - its not enough to force these values in the BIOS -
		 * they seem to be changed during PCI init as well.
		 */

		/* Change the PM registers to decode to the DD */

		rdmsr(0x510100e2, lo, hi);
		hi |= 0x80000000;
		wrmsr(0x510100e2, lo, hi);

		/* Change the ACPI registers to decode to the DD */

		rdmsr(0x510100e3, lo, hi);
		hi |= 0x80000000;
		wrmsr(0x510100e3, lo, hi);
	}

	/* GPIO24 controls WORK_AUX */

	geode_gpio_set(OLPC_GPIO_WORKAUX, GPIO_OUTPUT_ENABLE);
	geode_gpio_set(OLPC_GPIO_WORKAUX, GPIO_OUTPUT_AUX1);

	if (olpc_get_rev() >= OLPC_REV_B2) {
		/* GPIO10 is connected to the thermal alarm */
		geode_gpio_set(OLPC_GPIO_THRM_ALRM, GPIO_INPUT_ENABLE);
		geode_gpio_set(OLPC_GPIO_THRM_ALRM, GPIO_INPUT_AUX1);

		/* Set up to get LID events */
		geode_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_ENABLE);

		/* Clear edge detection and event enable for now */
		geode_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
		geode_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
		geode_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);

		geode_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
		geode_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);

		/* Set the LID to cause an PME event on group 6 */
		geode_gpio_event_pme(OLPC_GPIO_LID, 6);

		/* Set PME group 6 to fire the SCI interrupt */
		geode_gpio_set_irq(6, sci_irq);
	}

	geode_gpio_set(OLPC_GPIO_ECSCI, GPIO_INPUT_ENABLE);

	/* Clear pending events */

	geode_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
	geode_gpio_set(OLPC_GPIO_ECSCI, GPIO_POSITIVE_EDGE_STS);

	//geode_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_EN);
	geode_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);

	/* Set the SCI to cause a PME event on group 7 */
	geode_gpio_event_pme(OLPC_GPIO_ECSCI, 7);

	/* And have group 6 also fire the SCI interrupt */
	geode_gpio_set_irq(7, sci_irq);
}

/* This provides a control file for setting up testing of the
   power management system.  For now, there is just one setting:
   "test" which means that we don't actually enter the power
   off routine.
*/

static const char * const pm_states[] = {
	[PM_MODE_NORMAL]  = "normal",
	[PM_MODE_TEST]    = "test",
};

extern struct mutex pm_mutex;
extern struct kset power_subsys;

static ssize_t control_show(struct kset *s, char *buf)
{
	return sprintf(buf, "%s\n", pm_states[olpc_pm_mode]);
}

static ssize_t control_store(struct kset *s, const char *buf, size_t n)
{
	int i, len;
	char *p;

	p = memchr(buf, '\n', n);
	len = p ? p - buf : n;

	/* Grab the mutex */
	mutex_lock(&pm_mutex);
	
	for(i = 0; i < PM_MODE_MAX; i++) {
		if (!strncmp(buf, pm_states[i], len)) {
			olpc_pm_mode = i;
			break;
		}
	}

	mutex_unlock(&pm_mutex);

	return (i == PM_MODE_MAX) ? -EINVAL : n;
}
       
static struct subsys_attribute control_attr = {
	.attr = {
		.name = "olpc-pm",
		.mode = 0644,
	},
	.show = control_show,
	.store = control_store,
};

static struct attribute * olpc_attributes[] = {
	&control_attr.attr,
	NULL
};

static struct attribute_group olpc_attrs = {
	.attrs = olpc_attributes,
};

static int __init alloc_inputdevs(void)
{
	int ret = -ENOMEM;

	pm_inputdev = input_allocate_device();
	if (!pm_inputdev)
		goto err;

	pm_inputdev->name = "OLPC PM";
	pm_inputdev->phys = "olpc_pm/input0";
	set_bit(EV_KEY, pm_inputdev->evbit);
	set_bit(KEY_POWER, pm_inputdev->keybit);

	ret = input_register_device(pm_inputdev);
	if (ret) {
		printk(KERN_ERR "olpc-pm:  failed to register PM input device: %d\n", ret);
		goto err;
	}

	lid_inputdev = input_allocate_device();
	if (!lid_inputdev)
		goto err;

	lid_inputdev->name = "OLPC lid switch";
	lid_inputdev->phys = "olpc_pm/input1";
	set_bit(EV_SW, lid_inputdev->evbit);
	set_bit(SW_LID, lid_inputdev->swbit);

	ret = input_register_device(lid_inputdev);
	if (ret) {
		printk(KERN_ERR "olpc-pm:  failed to register lid input device: %d\n", ret);
		goto err;
	}

	ebook_inputdev = input_allocate_device();
	if (!ebook_inputdev)
		goto err;

	ebook_inputdev->name = "OLPC ebook switch";
	ebook_inputdev->phys = "olpc_pm/input2";
	set_bit(EV_SW, ebook_inputdev->evbit);
	set_bit(SW_TABLET_MODE, ebook_inputdev->swbit);

	ret = input_register_device(ebook_inputdev);
	if (ret) {
		printk(KERN_ERR "olpc-pm:  failed to register ebook input device: %d\n", ret);
		goto err;
	}

	return ret;
err:
	if (ebook_inputdev) {
		input_unregister_device(ebook_inputdev);
		ebook_inputdev = NULL;
	}
	if (lid_inputdev) {
		input_unregister_device(lid_inputdev);
		lid_inputdev = NULL;
	}
	if (pm_inputdev) {
		input_unregister_device(pm_inputdev);
		pm_inputdev = NULL;
	}

	return ret;
}

static int __init olpc_pm_init(void)
{
	uint32_t lo, hi;
	int ret;
	uint8_t ec_byte;

	if (!machine_is_olpc())
		return -ENODEV;

	acpi_base = geode_acpi_base();
	pms_base = geode_pms_base();

	if (!acpi_base || !pms_base)
	  return -ENODEV;

	ret = alloc_inputdevs();
	if (ret)
		return ret;

	rdmsr(0x51400020, lo, hi);
	sci_irq = (lo >> 20) & 15;

	if (sci_irq) {
		printk(KERN_INFO "SCI is mapped to IRQ %d\n", sci_irq);
	} else {
		/* Zero doesn't mean zero -- it means masked */
		printk(KERN_INFO "SCI unmapped. Mapping to IRQ 3\n");
		sci_irq = 3;
		lo |= 0x00300000;
		wrmsrl(0x51400020, lo);
	}

	olpc_fixup_bios();

	lo = inl(pms_base + PM_FSD);

	/* Lock, enable failsafe, 4 seconds */
	outl(0xc001f400, pms_base + PM_FSD);

	/* Here we set up the SCI events we're interested in during
	 * real-time.  We have no sleep button, and the RTC doesn't make
	 * sense, so set up the power button
	 */

	outl(inl(acpi_base) | ((CS5536_PM_PWRBTN) << 16), acpi_base);

	if (olpc_get_rev() >= OLPC_REV_B2) {
		gpio_wake_events |= GPIO_WAKEUP_LID;

		/* Get the current value of the GPIO, and set up the edges */
		olpc_lid_flag = geode_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);

		/* Watch for the opposite edge */

		if (olpc_lid_flag)
			geode_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
		else
			geode_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);

		/* Enable the event */
		geode_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
	}

	/* Set up the mask for wakeups the EC will generate SCIs on */

	ret = olpc_ec_cmd(EC_READ_SCI_MASK, NULL, 0, &ec_byte, 1);
	if (ret) 
		printk(KERN_ERR "Error getting the EC SCI mask: %d\n", ret);
	
	/* Disable battery 1% charge wakeups */
	ec_byte &= ~EC_SCI_SRC_BATSOC;

	ret = olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
	if (ret)
		printk(KERN_ERR "Error setting the EC SCI mask: %d\n", ret);

	/* Set up the EC SCI */

	gpio_wake_events |= GPIO_WAKEUP_EC;

	outl(gpio_wake_events, acpi_base + PM_GPE0_EN);
	outl(0xFFFFFFFF, acpi_base + PM_GPE0_STS);

	/* Select level triggered in PIC */

	if (sci_irq < 8) {
		lo = inb(0x4d0);
		lo |= 1 << sci_irq;
		outb(lo, 0x4d0);
	} else {
		lo = inb(0x4d1);
		lo |= 1 << (sci_irq - 8);
		outb(lo, 0x4d1);
	}
	/* Clear pending interrupt */
	outl(inl(acpi_base) | 0xFFFF, acpi_base);
	process_sci_queue(0);	/* we just want to flush the queue here */
	init_ebook_state();

	/* Enable the interrupt */

	ret = request_irq(sci_irq, &olpc_pm_interrupt, 0, "SCI", &acpi_base);

	if (ret) {
		printk(KERN_ERR "Error registering SCI: %d\n", ret);
		return ret;
	}

	ofw_bios_entry.address = 0xF0000 + PAGE_OFFSET;
	pm_set_ops(&olpc_pm_ops);

	sysfs_create_group(&power_subsys.kobj, &olpc_attrs);

	return 0;
}


#if defined (CONFIG_RTC_DRV_CMOS) || defined (CONFIG_RTC_DRV_CMOS_MODULE)
struct resource rtc_platform_resource[2] = {
	{
		.flags		= IORESOURCE_IO,
		.start		= RTC_PORT(0),
		.end		= RTC_PORT(0) + RTC_IO_EXTENT
	},
	{
		.flags = IORESOURCE_IRQ,
		.start = 8,
		.end = 8,
	},
};


static void rtc_wake_on(struct device *dev)
{
	olpc_wakeup_mask |= CS5536_PM_RTC;
}

static void rtc_wake_off(struct device *dev)
{
	olpc_wakeup_mask &= ~(CS5536_PM_RTC);
}

static struct cmos_rtc_board_info rtc_info = {
	.rtc_day_alarm = 0,
	.rtc_mon_alarm = 0,
	.rtc_century = 0,
	.wake_on = rtc_wake_on,
	.wake_off = rtc_wake_off,
};

struct platform_device olpc_rtc_device = {
	.name = "rtc_cmos",
	.id = -1,
	.num_resources = ARRAY_SIZE(rtc_platform_resource),
	.dev.platform_data = &rtc_info,
	.resource = rtc_platform_resource,
};

static int __init olpc_platform_init(void)
{
	(void)platform_device_register(&olpc_rtc_device);
	device_init_wakeup(&olpc_rtc_device.dev, 1);

	(void)platform_device_register(&olpc_powerbutton_dev);
	device_init_wakeup(&olpc_powerbutton_dev.dev, 1);

	(void)platform_device_register(&olpc_lid_dev);
	device_init_wakeup(&olpc_lid_dev.dev, 1);

	return 0;
}
arch_initcall(olpc_platform_init);
#endif /* CONFIG_RTC_DRV_CMOS */

static void olpc_pm_exit(void)
{
	/* Clear any pending events, and disable them */
	outl(0xFFFF, acpi_base+2);

	free_irq(sci_irq, &acpi_base);
	input_unregister_device(pm_inputdev);
	input_unregister_device(lid_inputdev);
	input_unregister_device(ebook_inputdev);
}

static struct pm_ops olpc_pm_ops = {
        .valid = olpc_pm_state_valid,
        .enter = olpc_pm_enter,
};

module_init(olpc_pm_init);
module_exit(olpc_pm_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
MODULE_DESCRIPTION("AMD Geode power management for OLPC CL1");