bootmem: respect goal more likely
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / macintosh / mediabay.c
blobb1e5b4705250472a2819da9537fff2422e3ef967
1 /*
2 * Driver for the media bay on the PowerBook 3400 and 2400.
4 * Copyright (C) 1998 Paul Mackerras.
6 * Various evolutions by Benjamin Herrenschmidt & Henry Worth
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/sched.h>
18 #include <linux/timer.h>
19 #include <linux/hdreg.h>
20 #include <linux/stddef.h>
21 #include <linux/init.h>
22 #include <linux/ide.h>
23 #include <linux/kthread.h>
24 #include <linux/mutex.h>
25 #include <asm/prom.h>
26 #include <asm/pgtable.h>
27 #include <asm/io.h>
28 #include <asm/machdep.h>
29 #include <asm/pmac_feature.h>
30 #include <asm/mediabay.h>
31 #include <asm/sections.h>
32 #include <asm/ohare.h>
33 #include <asm/heathrow.h>
34 #include <asm/keylargo.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
39 #define MB_DEBUG
41 #ifdef MB_DEBUG
42 #define MBDBG(fmt, arg...) printk(KERN_INFO fmt , ## arg)
43 #else
44 #define MBDBG(fmt, arg...) do { } while (0)
45 #endif
47 #define MB_FCR32(bay, r) ((bay)->base + ((r) >> 2))
48 #define MB_FCR8(bay, r) (((volatile u8 __iomem *)((bay)->base)) + (r))
50 #define MB_IN32(bay,r) (in_le32(MB_FCR32(bay,r)))
51 #define MB_OUT32(bay,r,v) (out_le32(MB_FCR32(bay,r), (v)))
52 #define MB_BIS(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
53 #define MB_BIC(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
54 #define MB_IN8(bay,r) (in_8(MB_FCR8(bay,r)))
55 #define MB_OUT8(bay,r,v) (out_8(MB_FCR8(bay,r), (v)))
57 struct media_bay_info;
59 struct mb_ops {
60 char* name;
61 void (*init)(struct media_bay_info *bay);
62 u8 (*content)(struct media_bay_info *bay);
63 void (*power)(struct media_bay_info *bay, int on_off);
64 int (*setup_bus)(struct media_bay_info *bay, u8 device_id);
65 void (*un_reset)(struct media_bay_info *bay);
66 void (*un_reset_ide)(struct media_bay_info *bay);
69 struct media_bay_info {
70 u32 __iomem *base;
71 int content_id;
72 int state;
73 int last_value;
74 int value_count;
75 int timer;
76 struct macio_dev *mdev;
77 struct mb_ops* ops;
78 int index;
79 int cached_gpio;
80 int sleeping;
81 struct mutex lock;
82 #ifdef CONFIG_BLK_DEV_IDE_PMAC
83 ide_hwif_t *cd_port;
84 void __iomem *cd_base;
85 int cd_irq;
86 int cd_retry;
87 #endif
88 #if defined(CONFIG_BLK_DEV_IDE_PMAC)
89 int cd_index;
90 #endif
93 #define MAX_BAYS 2
95 static struct media_bay_info media_bays[MAX_BAYS];
96 int media_bay_count = 0;
98 #ifdef CONFIG_BLK_DEV_IDE_PMAC
99 /* check the busy bit in the media-bay ide interface
100 (assumes the media-bay contains an ide device) */
101 #define MB_IDE_READY(i) ((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
102 #endif
105 * Wait that number of ms between each step in normal polling mode
107 #define MB_POLL_DELAY 25
110 * Consider the media-bay ID value stable if it is the same for
111 * this number of milliseconds
113 #define MB_STABLE_DELAY 100
115 /* Wait after powering up the media bay this delay in ms
116 * timeout bumped for some powerbooks
118 #define MB_POWER_DELAY 200
121 * Hold the media-bay reset signal true for this many ticks
122 * after a device is inserted before releasing it.
124 #define MB_RESET_DELAY 50
127 * Wait this long after the reset signal is released and before doing
128 * further operations. After this delay, the IDE reset signal is released
129 * too for an IDE device
131 #define MB_SETUP_DELAY 100
134 * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
135 * (or until the device is ready) before waiting for busy bit to disappear
137 #define MB_IDE_WAIT 1000
140 * Timeout waiting for busy bit of an IDE device to go down
142 #define MB_IDE_TIMEOUT 5000
145 * Max retries of the full power up/down sequence for an IDE device
147 #define MAX_CD_RETRIES 3
150 * States of a media bay
152 enum {
153 mb_empty = 0, /* Idle */
154 mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
155 mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
156 mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
157 mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
158 mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
159 mb_up, /* Media bay full */
160 mb_powering_down /* Powering down (avoid too fast down/up) */
163 #define MB_POWER_SOUND 0x08
164 #define MB_POWER_FLOPPY 0x04
165 #define MB_POWER_ATA 0x02
166 #define MB_POWER_PCI 0x01
167 #define MB_POWER_OFF 0x00
170 * Functions for polling content of media bay
173 static u8
174 ohare_mb_content(struct media_bay_info *bay)
176 return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
179 static u8
180 heathrow_mb_content(struct media_bay_info *bay)
182 return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
185 static u8
186 keylargo_mb_content(struct media_bay_info *bay)
188 int new_gpio;
190 new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
191 if (new_gpio) {
192 bay->cached_gpio = new_gpio;
193 return MB_NO;
194 } else if (bay->cached_gpio != new_gpio) {
195 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
196 (void)MB_IN32(bay, KEYLARGO_MBCR);
197 udelay(5);
198 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
199 (void)MB_IN32(bay, KEYLARGO_MBCR);
200 udelay(5);
201 bay->cached_gpio = new_gpio;
203 return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
207 * Functions for powering up/down the bay, puts the bay device
208 * into reset state as well
211 static void
212 ohare_mb_power(struct media_bay_info* bay, int on_off)
214 if (on_off) {
215 /* Power up device, assert it's reset line */
216 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
217 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
218 } else {
219 /* Disable all devices */
220 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
221 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
222 /* Cut power from bay, release reset line */
223 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
224 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
225 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
227 MB_BIC(bay, OHARE_MBCR, 0x00000F00);
230 static void
231 heathrow_mb_power(struct media_bay_info* bay, int on_off)
233 if (on_off) {
234 /* Power up device, assert it's reset line */
235 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
236 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
237 } else {
238 /* Disable all devices */
239 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
240 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
241 /* Cut power from bay, release reset line */
242 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
243 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
244 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
246 MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
249 static void
250 keylargo_mb_power(struct media_bay_info* bay, int on_off)
252 if (on_off) {
253 /* Power up device, assert it's reset line */
254 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
255 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
256 } else {
257 /* Disable all devices */
258 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
259 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
260 /* Cut power from bay, release reset line */
261 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
262 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
263 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
265 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
269 * Functions for configuring the media bay for a given type of device,
270 * enable the related busses
273 static int
274 ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
276 switch(device_id) {
277 case MB_FD:
278 case MB_FD1:
279 MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
280 MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
281 return 0;
282 case MB_CD:
283 MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
284 MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
285 return 0;
286 case MB_PCI:
287 MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
288 return 0;
290 return -ENODEV;
293 static int
294 heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
296 switch(device_id) {
297 case MB_FD:
298 case MB_FD1:
299 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
300 MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
301 return 0;
302 case MB_CD:
303 MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
304 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
305 return 0;
306 case MB_PCI:
307 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
308 return 0;
310 return -ENODEV;
313 static int
314 keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
316 switch(device_id) {
317 case MB_CD:
318 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
319 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
320 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
321 return 0;
322 case MB_PCI:
323 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
324 return 0;
325 case MB_SOUND:
326 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
327 return 0;
329 return -ENODEV;
333 * Functions for tweaking resets
336 static void
337 ohare_mb_un_reset(struct media_bay_info* bay)
339 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
342 static void keylargo_mb_init(struct media_bay_info *bay)
344 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
347 static void heathrow_mb_un_reset(struct media_bay_info* bay)
349 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
352 static void keylargo_mb_un_reset(struct media_bay_info* bay)
354 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
357 static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
359 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
362 static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
364 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
367 static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
369 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
372 static inline void set_mb_power(struct media_bay_info* bay, int onoff)
374 /* Power up up and assert the bay reset line */
375 if (onoff) {
376 bay->ops->power(bay, 1);
377 bay->state = mb_powering_up;
378 MBDBG("mediabay%d: powering up\n", bay->index);
379 } else {
380 /* Make sure everything is powered down & disabled */
381 bay->ops->power(bay, 0);
382 bay->state = mb_powering_down;
383 MBDBG("mediabay%d: powering down\n", bay->index);
385 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
388 static void poll_media_bay(struct media_bay_info* bay)
390 int id = bay->ops->content(bay);
392 if (id == bay->last_value) {
393 if (id != bay->content_id) {
394 bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
395 if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
396 /* If the device type changes without going thru
397 * "MB_NO", we force a pass by "MB_NO" to make sure
398 * things are properly reset
400 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
401 id = MB_NO;
402 MBDBG("mediabay%d: forcing MB_NO\n", bay->index);
404 MBDBG("mediabay%d: switching to %d\n", bay->index, id);
405 set_mb_power(bay, id != MB_NO);
406 bay->content_id = id;
407 if (id == MB_NO) {
408 #ifdef CONFIG_BLK_DEV_IDE_PMAC
409 bay->cd_retry = 0;
410 #endif
411 printk(KERN_INFO "media bay %d is empty\n", bay->index);
415 } else {
416 bay->last_value = id;
417 bay->value_count = 0;
421 #ifdef CONFIG_BLK_DEV_IDE_PMAC
422 int check_media_bay(struct device_node *which_bay, int what)
424 int i;
426 for (i=0; i<media_bay_count; i++)
427 if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) {
428 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
429 return 0;
430 media_bays[i].cd_index = -1;
431 return -EINVAL;
433 return -ENODEV;
435 EXPORT_SYMBOL(check_media_bay);
437 int check_media_bay_by_base(unsigned long base, int what)
439 int i;
441 for (i=0; i<media_bay_count; i++)
442 if (media_bays[i].mdev && base == (unsigned long) media_bays[i].cd_base) {
443 if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
444 return 0;
445 media_bays[i].cd_index = -1;
446 return -EINVAL;
449 return -ENODEV;
452 int media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
453 int irq, ide_hwif_t *hwif)
455 int i;
457 for (i=0; i<media_bay_count; i++) {
458 struct media_bay_info* bay = &media_bays[i];
460 if (bay->mdev && which_bay == bay->mdev->ofdev.node) {
461 int timeout = 5000, index = hwif->index;
463 mutex_lock(&bay->lock);
465 bay->cd_port = hwif;
466 bay->cd_base = (void __iomem *) base;
467 bay->cd_irq = irq;
469 if ((MB_CD != bay->content_id) || bay->state != mb_up) {
470 mutex_unlock(&bay->lock);
471 return 0;
473 printk(KERN_DEBUG "Registered ide%d for media bay %d\n", index, i);
474 do {
475 if (MB_IDE_READY(i)) {
476 bay->cd_index = index;
477 mutex_unlock(&bay->lock);
478 return 0;
480 mdelay(1);
481 } while(--timeout);
482 printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i);
483 mutex_unlock(&bay->lock);
484 return -ENODEV;
488 return -ENODEV;
490 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
492 static void media_bay_step(int i)
494 struct media_bay_info* bay = &media_bays[i];
496 /* We don't poll when powering down */
497 if (bay->state != mb_powering_down)
498 poll_media_bay(bay);
500 /* If timer expired or polling IDE busy, run state machine */
501 if ((bay->state != mb_ide_waiting) && (bay->timer != 0)) {
502 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
503 if (bay->timer > 0)
504 return;
505 bay->timer = 0;
508 switch(bay->state) {
509 case mb_powering_up:
510 if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
511 MBDBG("mediabay%d: device not supported (kind:%d)\n", i, bay->content_id);
512 set_mb_power(bay, 0);
513 break;
515 bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
516 bay->state = mb_enabling_bay;
517 MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
518 break;
519 case mb_enabling_bay:
520 bay->ops->un_reset(bay);
521 bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
522 bay->state = mb_resetting;
523 MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
524 break;
525 case mb_resetting:
526 if (bay->content_id != MB_CD) {
527 MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
528 bay->state = mb_up;
529 break;
531 #ifdef CONFIG_BLK_DEV_IDE_PMAC
532 MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
533 bay->ops->un_reset_ide(bay);
534 bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
535 bay->state = mb_ide_resetting;
536 #else
537 printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
538 set_mb_power(bay, 0);
539 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
540 break;
541 #ifdef CONFIG_BLK_DEV_IDE_PMAC
542 case mb_ide_resetting:
543 bay->timer = msecs_to_jiffies(MB_IDE_TIMEOUT);
544 bay->state = mb_ide_waiting;
545 MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
546 break;
547 case mb_ide_waiting:
548 if (bay->cd_base == NULL) {
549 bay->timer = 0;
550 bay->state = mb_up;
551 MBDBG("mediabay%d: up before IDE init\n", i);
552 break;
553 } else if (MB_IDE_READY(i)) {
554 bay->timer = 0;
555 bay->state = mb_up;
556 if (bay->cd_index < 0) {
557 printk("mediabay %d, registering IDE...\n", i);
558 pmu_suspend();
559 ide_port_scan(bay->cd_port);
560 if (bay->cd_port->present)
561 bay->cd_index = bay->cd_port->index;
562 pmu_resume();
564 if (bay->cd_index == -1) {
565 /* We eventually do a retry */
566 bay->cd_retry++;
567 printk("IDE register error\n");
568 set_mb_power(bay, 0);
569 } else {
570 printk(KERN_DEBUG "media-bay %d is ide%d\n", i, bay->cd_index);
571 MBDBG("mediabay %d IDE ready\n", i);
573 break;
574 } else if (bay->timer > 0)
575 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
576 if (bay->timer <= 0) {
577 printk("\nIDE Timeout in bay %d !, IDE state is: 0x%02x\n",
578 i, readb(bay->cd_base + 0x70));
579 MBDBG("mediabay%d: nIDE Timeout !\n", i);
580 set_mb_power(bay, 0);
581 bay->timer = 0;
583 break;
584 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
585 case mb_powering_down:
586 bay->state = mb_empty;
587 #ifdef CONFIG_BLK_DEV_IDE_PMAC
588 if (bay->cd_index >= 0) {
589 printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
590 bay->cd_index);
591 ide_port_unregister_devices(bay->cd_port);
592 bay->cd_index = -1;
594 if (bay->cd_retry) {
595 if (bay->cd_retry > MAX_CD_RETRIES) {
596 /* Should add an error sound (sort of beep in dmasound) */
597 printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
598 } else {
599 /* Force a new power down/up sequence */
600 bay->content_id = MB_NO;
603 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
604 MBDBG("mediabay%d: end of power down\n", i);
605 break;
610 * This procedure runs as a kernel thread to poll the media bay
611 * once each tick and register and unregister the IDE interface
612 * with the IDE driver. It needs to be a thread because
613 * ide_register can't be called from interrupt context.
615 static int media_bay_task(void *x)
617 int i;
619 while (!kthread_should_stop()) {
620 for (i = 0; i < media_bay_count; ++i) {
621 mutex_lock(&media_bays[i].lock);
622 if (!media_bays[i].sleeping)
623 media_bay_step(i);
624 mutex_unlock(&media_bays[i].lock);
627 msleep_interruptible(MB_POLL_DELAY);
629 return 0;
632 static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_device_id *match)
634 struct media_bay_info* bay;
635 u32 __iomem *regbase;
636 struct device_node *ofnode;
637 unsigned long base;
638 int i;
640 ofnode = mdev->ofdev.node;
642 if (macio_resource_count(mdev) < 1)
643 return -ENODEV;
644 if (macio_request_resources(mdev, "media-bay"))
645 return -EBUSY;
646 /* Media bay registers are located at the beginning of the
647 * mac-io chip, for now, we trick and align down the first
648 * resource passed in
650 base = macio_resource_start(mdev, 0) & 0xffff0000u;
651 regbase = (u32 __iomem *)ioremap(base, 0x100);
652 if (regbase == NULL) {
653 macio_release_resources(mdev);
654 return -ENOMEM;
657 i = media_bay_count++;
658 bay = &media_bays[i];
659 bay->mdev = mdev;
660 bay->base = regbase;
661 bay->index = i;
662 bay->ops = match->data;
663 bay->sleeping = 0;
664 mutex_init(&bay->lock);
666 /* Init HW probing */
667 if (bay->ops->init)
668 bay->ops->init(bay);
670 printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
672 /* Force an immediate detect */
673 set_mb_power(bay, 0);
674 msleep(MB_POWER_DELAY);
675 bay->content_id = MB_NO;
676 bay->last_value = bay->ops->content(bay);
677 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
678 bay->state = mb_empty;
679 do {
680 msleep(MB_POLL_DELAY);
681 media_bay_step(i);
682 } while((bay->state != mb_empty) &&
683 (bay->state != mb_up));
685 /* Mark us ready by filling our mdev data */
686 macio_set_drvdata(mdev, bay);
688 /* Startup kernel thread */
689 if (i == 0)
690 kthread_run(media_bay_task, NULL, "media-bay");
692 return 0;
696 static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
698 struct media_bay_info *bay = macio_get_drvdata(mdev);
700 if (state.event != mdev->ofdev.dev.power.power_state.event
701 && (state.event & PM_EVENT_SLEEP)) {
702 mutex_lock(&bay->lock);
703 bay->sleeping = 1;
704 set_mb_power(bay, 0);
705 mutex_unlock(&bay->lock);
706 msleep(MB_POLL_DELAY);
707 mdev->ofdev.dev.power.power_state = state;
709 return 0;
712 static int media_bay_resume(struct macio_dev *mdev)
714 struct media_bay_info *bay = macio_get_drvdata(mdev);
716 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
717 mdev->ofdev.dev.power.power_state = PMSG_ON;
719 /* We re-enable the bay using it's previous content
720 only if it did not change. Note those bozo timings,
721 they seem to help the 3400 get it right.
723 /* Force MB power to 0 */
724 mutex_lock(&bay->lock);
725 set_mb_power(bay, 0);
726 msleep(MB_POWER_DELAY);
727 if (bay->ops->content(bay) != bay->content_id) {
728 printk("mediabay%d: content changed during sleep...\n", bay->index);
729 mutex_unlock(&bay->lock);
730 return 0;
732 set_mb_power(bay, 1);
733 bay->last_value = bay->content_id;
734 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
735 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
736 #ifdef CONFIG_BLK_DEV_IDE_PMAC
737 bay->cd_retry = 0;
738 #endif
739 do {
740 msleep(MB_POLL_DELAY);
741 media_bay_step(bay->index);
742 } while((bay->state != mb_empty) &&
743 (bay->state != mb_up));
744 bay->sleeping = 0;
745 mutex_unlock(&bay->lock);
747 return 0;
751 /* Definitions of "ops" structures.
753 static struct mb_ops ohare_mb_ops = {
754 .name = "Ohare",
755 .content = ohare_mb_content,
756 .power = ohare_mb_power,
757 .setup_bus = ohare_mb_setup_bus,
758 .un_reset = ohare_mb_un_reset,
759 .un_reset_ide = ohare_mb_un_reset_ide,
762 static struct mb_ops heathrow_mb_ops = {
763 .name = "Heathrow",
764 .content = heathrow_mb_content,
765 .power = heathrow_mb_power,
766 .setup_bus = heathrow_mb_setup_bus,
767 .un_reset = heathrow_mb_un_reset,
768 .un_reset_ide = heathrow_mb_un_reset_ide,
771 static struct mb_ops keylargo_mb_ops = {
772 .name = "KeyLargo",
773 .init = keylargo_mb_init,
774 .content = keylargo_mb_content,
775 .power = keylargo_mb_power,
776 .setup_bus = keylargo_mb_setup_bus,
777 .un_reset = keylargo_mb_un_reset,
778 .un_reset_ide = keylargo_mb_un_reset_ide,
782 * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
783 * register is always set when there is something in the media bay.
784 * This causes problems for the interrupt code if we attach an interrupt
785 * handler to the media-bay interrupt, because it tends to go into
786 * an infinite loop calling the media bay interrupt handler.
787 * Therefore we do it all by polling the media bay once each tick.
790 static struct of_device_id media_bay_match[] =
793 .name = "media-bay",
794 .compatible = "keylargo-media-bay",
795 .data = &keylargo_mb_ops,
798 .name = "media-bay",
799 .compatible = "heathrow-media-bay",
800 .data = &heathrow_mb_ops,
803 .name = "media-bay",
804 .compatible = "ohare-media-bay",
805 .data = &ohare_mb_ops,
810 static struct macio_driver media_bay_driver =
812 .name = "media-bay",
813 .match_table = media_bay_match,
814 .probe = media_bay_attach,
815 .suspend = media_bay_suspend,
816 .resume = media_bay_resume
819 static int __init media_bay_init(void)
821 int i;
823 for (i=0; i<MAX_BAYS; i++) {
824 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
825 media_bays[i].content_id = -1;
826 #ifdef CONFIG_BLK_DEV_IDE_PMAC
827 media_bays[i].cd_index = -1;
828 #endif
830 if (!machine_is(powermac))
831 return 0;
833 macio_register_driver(&media_bay_driver);
835 return 0;
838 device_initcall(media_bay_init);