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/stddef.h>
20 #include <linux/init.h>
21 #include <linux/kthread.h>
22 #include <linux/mutex.h>
24 #include <asm/pgtable.h>
26 #include <asm/machdep.h>
27 #include <asm/pmac_feature.h>
28 #include <asm/mediabay.h>
29 #include <asm/sections.h>
30 #include <asm/ohare.h>
31 #include <asm/heathrow.h>
32 #include <asm/keylargo.h>
33 #include <linux/adb.h>
34 #include <linux/pmu.h>
36 #define MB_FCR32(bay, r) ((bay)->base + ((r) >> 2))
37 #define MB_FCR8(bay, r) (((volatile u8 __iomem *)((bay)->base)) + (r))
39 #define MB_IN32(bay,r) (in_le32(MB_FCR32(bay,r)))
40 #define MB_OUT32(bay,r,v) (out_le32(MB_FCR32(bay,r), (v)))
41 #define MB_BIS(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
42 #define MB_BIC(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
43 #define MB_IN8(bay,r) (in_8(MB_FCR8(bay,r)))
44 #define MB_OUT8(bay,r,v) (out_8(MB_FCR8(bay,r), (v)))
46 struct media_bay_info;
50 void (*init)(struct media_bay_info *bay);
51 u8 (*content)(struct media_bay_info *bay);
52 void (*power)(struct media_bay_info *bay, int on_off);
53 int (*setup_bus)(struct media_bay_info *bay, u8 device_id);
54 void (*un_reset)(struct media_bay_info *bay);
55 void (*un_reset_ide)(struct media_bay_info *bay);
58 struct media_bay_info {
65 struct macio_dev *mdev;
76 static struct media_bay_info media_bays[MAX_BAYS];
77 static int media_bay_count = 0;
80 * Wait that number of ms between each step in normal polling mode
82 #define MB_POLL_DELAY 25
85 * Consider the media-bay ID value stable if it is the same for
86 * this number of milliseconds
88 #define MB_STABLE_DELAY 100
90 /* Wait after powering up the media bay this delay in ms
91 * timeout bumped for some powerbooks
93 #define MB_POWER_DELAY 200
96 * Hold the media-bay reset signal true for this many ticks
97 * after a device is inserted before releasing it.
99 #define MB_RESET_DELAY 50
102 * Wait this long after the reset signal is released and before doing
103 * further operations. After this delay, the IDE reset signal is released
104 * too for an IDE device
106 #define MB_SETUP_DELAY 100
109 * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
110 * (or until the device is ready) before calling into the driver
112 #define MB_IDE_WAIT 1000
115 * States of a media bay
118 mb_empty = 0, /* Idle */
119 mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
120 mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
121 mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
122 mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
123 mb_up, /* Media bay full */
124 mb_powering_down /* Powering down (avoid too fast down/up) */
127 #define MB_POWER_SOUND 0x08
128 #define MB_POWER_FLOPPY 0x04
129 #define MB_POWER_ATA 0x02
130 #define MB_POWER_PCI 0x01
131 #define MB_POWER_OFF 0x00
134 * Functions for polling content of media bay
138 ohare_mb_content(struct media_bay_info *bay)
140 return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
144 heathrow_mb_content(struct media_bay_info *bay)
146 return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
150 keylargo_mb_content(struct media_bay_info *bay)
154 new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
156 bay->cached_gpio = new_gpio;
158 } else if (bay->cached_gpio != new_gpio) {
159 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
160 (void)MB_IN32(bay, KEYLARGO_MBCR);
162 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
163 (void)MB_IN32(bay, KEYLARGO_MBCR);
165 bay->cached_gpio = new_gpio;
167 return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
171 * Functions for powering up/down the bay, puts the bay device
172 * into reset state as well
176 ohare_mb_power(struct media_bay_info* bay, int on_off)
179 /* Power up device, assert it's reset line */
180 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
181 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
183 /* Disable all devices */
184 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
185 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
186 /* Cut power from bay, release reset line */
187 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
188 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
189 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
191 MB_BIC(bay, OHARE_MBCR, 0x00000F00);
195 heathrow_mb_power(struct media_bay_info* bay, int on_off)
198 /* Power up device, assert it's reset line */
199 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
200 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
202 /* Disable all devices */
203 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
204 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
205 /* Cut power from bay, release reset line */
206 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
207 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
208 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
210 MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
214 keylargo_mb_power(struct media_bay_info* bay, int on_off)
217 /* Power up device, assert it's reset line */
218 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
219 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
221 /* Disable all devices */
222 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
223 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
224 /* Cut power from bay, release reset line */
225 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
226 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
227 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
229 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
233 * Functions for configuring the media bay for a given type of device,
234 * enable the related busses
238 ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
243 MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
244 MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
247 MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
248 MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
251 MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
258 heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
263 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
264 MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
267 MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
268 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
271 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
278 keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
282 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
283 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
284 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
287 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
290 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
297 * Functions for tweaking resets
301 ohare_mb_un_reset(struct media_bay_info* bay)
303 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
306 static void keylargo_mb_init(struct media_bay_info *bay)
308 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
311 static void heathrow_mb_un_reset(struct media_bay_info* bay)
313 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
316 static void keylargo_mb_un_reset(struct media_bay_info* bay)
318 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
321 static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
323 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
326 static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
328 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
331 static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
333 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
336 static inline void set_mb_power(struct media_bay_info* bay, int onoff)
338 /* Power up up and assert the bay reset line */
340 bay->ops->power(bay, 1);
341 bay->state = mb_powering_up;
342 pr_debug("mediabay%d: powering up\n", bay->index);
344 /* Make sure everything is powered down & disabled */
345 bay->ops->power(bay, 0);
346 bay->state = mb_powering_down;
347 pr_debug("mediabay%d: powering down\n", bay->index);
349 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
352 static void poll_media_bay(struct media_bay_info* bay)
354 int id = bay->ops->content(bay);
356 static char *mb_content_types[] = {
359 "an unsuported audio device",
361 "an unsupported PCI device",
365 if (id != bay->last_value) {
366 bay->last_value = id;
367 bay->value_count = 0;
370 if (id == bay->content_id)
373 bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
374 if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
375 /* If the device type changes without going thru
376 * "MB_NO", we force a pass by "MB_NO" to make sure
377 * things are properly reset
379 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
381 pr_debug("mediabay%d: forcing MB_NO\n", bay->index);
383 pr_debug("mediabay%d: switching to %d\n", bay->index, id);
384 set_mb_power(bay, id != MB_NO);
385 bay->content_id = id;
386 if (id >= MB_NO || id < 0)
387 printk(KERN_INFO "mediabay%d: Bay is now empty\n", bay->index);
389 printk(KERN_INFO "mediabay%d: Bay contains %s\n",
390 bay->index, mb_content_types[id]);
394 int check_media_bay(struct macio_dev *baydev)
396 struct media_bay_info* bay;
402 /* This returns an instant snapshot, not locking, sine
403 * we may be called with the bay lock held. The resulting
404 * fuzzyness of the result if called at the wrong time is
405 * not actually a huge deal
407 bay = macio_get_drvdata(baydev);
410 id = bay->content_id;
411 if (bay->state != mb_up)
417 EXPORT_SYMBOL_GPL(check_media_bay);
419 void lock_media_bay(struct macio_dev *baydev)
421 struct media_bay_info* bay;
425 bay = macio_get_drvdata(baydev);
428 mutex_lock(&bay->lock);
431 EXPORT_SYMBOL_GPL(lock_media_bay);
433 void unlock_media_bay(struct macio_dev *baydev)
435 struct media_bay_info* bay;
439 bay = macio_get_drvdata(baydev);
442 if (bay->user_lock) {
444 mutex_unlock(&bay->lock);
447 EXPORT_SYMBOL_GPL(unlock_media_bay);
449 static int mb_broadcast_hotplug(struct device *dev, void *data)
451 struct media_bay_info* bay = data;
452 struct macio_dev *mdev;
453 struct macio_driver *drv;
456 if (dev->bus != &macio_bus_type)
459 state = bay->state == mb_up ? bay->content_id : MB_NO;
462 mdev = to_macio_device(dev);
463 drv = to_macio_driver(dev->driver);
464 if (dev->driver && drv->mediabay_event)
465 drv->mediabay_event(mdev, state);
469 static void media_bay_step(int i)
471 struct media_bay_info* bay = &media_bays[i];
473 /* We don't poll when powering down */
474 if (bay->state != mb_powering_down)
477 /* If timer expired run state machine */
478 if (bay->timer != 0) {
479 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
487 if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
488 pr_debug("mediabay%d: device not supported (kind:%d)\n",
490 set_mb_power(bay, 0);
493 bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
494 bay->state = mb_enabling_bay;
495 pr_debug("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
497 case mb_enabling_bay:
498 bay->ops->un_reset(bay);
499 bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
500 bay->state = mb_resetting;
501 pr_debug("mediabay%d: releasing bay reset (kind:%d)\n",
505 if (bay->content_id != MB_CD) {
506 pr_debug("mediabay%d: bay is up (kind:%d)\n", i,
509 device_for_each_child(&bay->mdev->ofdev.dev,
510 bay, mb_broadcast_hotplug);
513 pr_debug("mediabay%d: releasing ATA reset (kind:%d)\n",
515 bay->ops->un_reset_ide(bay);
516 bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
517 bay->state = mb_ide_resetting;
520 case mb_ide_resetting:
521 pr_debug("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
523 device_for_each_child(&bay->mdev->ofdev.dev,
524 bay, mb_broadcast_hotplug);
527 case mb_powering_down:
528 bay->state = mb_empty;
529 device_for_each_child(&bay->mdev->ofdev.dev,
530 bay, mb_broadcast_hotplug);
531 pr_debug("mediabay%d: end of power down\n", i);
537 * This procedure runs as a kernel thread to poll the media bay
538 * once each tick and register and unregister the IDE interface
539 * with the IDE driver. It needs to be a thread because
540 * ide_register can't be called from interrupt context.
542 static int media_bay_task(void *x)
546 while (!kthread_should_stop()) {
547 for (i = 0; i < media_bay_count; ++i) {
548 mutex_lock(&media_bays[i].lock);
549 if (!media_bays[i].sleeping)
551 mutex_unlock(&media_bays[i].lock);
554 msleep_interruptible(MB_POLL_DELAY);
559 static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_device_id *match)
561 struct media_bay_info* bay;
562 u32 __iomem *regbase;
563 struct device_node *ofnode;
567 ofnode = mdev->ofdev.dev.of_node;
569 if (macio_resource_count(mdev) < 1)
571 if (macio_request_resources(mdev, "media-bay"))
573 /* Media bay registers are located at the beginning of the
574 * mac-io chip, for now, we trick and align down the first
577 base = macio_resource_start(mdev, 0) & 0xffff0000u;
578 regbase = (u32 __iomem *)ioremap(base, 0x100);
579 if (regbase == NULL) {
580 macio_release_resources(mdev);
584 i = media_bay_count++;
585 bay = &media_bays[i];
589 bay->ops = match->data;
591 mutex_init(&bay->lock);
593 /* Init HW probing */
597 printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
599 /* Force an immediate detect */
600 set_mb_power(bay, 0);
601 msleep(MB_POWER_DELAY);
602 bay->content_id = MB_NO;
603 bay->last_value = bay->ops->content(bay);
604 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
605 bay->state = mb_empty;
607 /* Mark us ready by filling our mdev data */
608 macio_set_drvdata(mdev, bay);
610 /* Startup kernel thread */
612 kthread_run(media_bay_task, NULL, "media-bay");
618 static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
620 struct media_bay_info *bay = macio_get_drvdata(mdev);
622 if (state.event != mdev->ofdev.dev.power.power_state.event
623 && (state.event & PM_EVENT_SLEEP)) {
624 mutex_lock(&bay->lock);
626 set_mb_power(bay, 0);
627 mutex_unlock(&bay->lock);
628 msleep(MB_POLL_DELAY);
629 mdev->ofdev.dev.power.power_state = state;
634 static int media_bay_resume(struct macio_dev *mdev)
636 struct media_bay_info *bay = macio_get_drvdata(mdev);
638 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
639 mdev->ofdev.dev.power.power_state = PMSG_ON;
641 /* We re-enable the bay using it's previous content
642 only if it did not change. Note those bozo timings,
643 they seem to help the 3400 get it right.
645 /* Force MB power to 0 */
646 mutex_lock(&bay->lock);
647 set_mb_power(bay, 0);
648 msleep(MB_POWER_DELAY);
649 if (bay->ops->content(bay) != bay->content_id) {
650 printk("mediabay%d: Content changed during sleep...\n", bay->index);
651 mutex_unlock(&bay->lock);
654 set_mb_power(bay, 1);
655 bay->last_value = bay->content_id;
656 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
657 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
659 msleep(MB_POLL_DELAY);
660 media_bay_step(bay->index);
661 } while((bay->state != mb_empty) &&
662 (bay->state != mb_up));
664 mutex_unlock(&bay->lock);
670 /* Definitions of "ops" structures.
672 static struct mb_ops ohare_mb_ops = {
674 .content = ohare_mb_content,
675 .power = ohare_mb_power,
676 .setup_bus = ohare_mb_setup_bus,
677 .un_reset = ohare_mb_un_reset,
678 .un_reset_ide = ohare_mb_un_reset_ide,
681 static struct mb_ops heathrow_mb_ops = {
683 .content = heathrow_mb_content,
684 .power = heathrow_mb_power,
685 .setup_bus = heathrow_mb_setup_bus,
686 .un_reset = heathrow_mb_un_reset,
687 .un_reset_ide = heathrow_mb_un_reset_ide,
690 static struct mb_ops keylargo_mb_ops = {
692 .init = keylargo_mb_init,
693 .content = keylargo_mb_content,
694 .power = keylargo_mb_power,
695 .setup_bus = keylargo_mb_setup_bus,
696 .un_reset = keylargo_mb_un_reset,
697 .un_reset_ide = keylargo_mb_un_reset_ide,
701 * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
702 * register is always set when there is something in the media bay.
703 * This causes problems for the interrupt code if we attach an interrupt
704 * handler to the media-bay interrupt, because it tends to go into
705 * an infinite loop calling the media bay interrupt handler.
706 * Therefore we do it all by polling the media bay once each tick.
709 static struct of_device_id media_bay_match[] =
713 .compatible = "keylargo-media-bay",
714 .data = &keylargo_mb_ops,
718 .compatible = "heathrow-media-bay",
719 .data = &heathrow_mb_ops,
723 .compatible = "ohare-media-bay",
724 .data = &ohare_mb_ops,
729 static struct macio_driver media_bay_driver =
733 .of_match_table = media_bay_match,
735 .probe = media_bay_attach,
736 .suspend = media_bay_suspend,
737 .resume = media_bay_resume
740 static int __init media_bay_init(void)
744 for (i=0; i<MAX_BAYS; i++) {
745 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
746 media_bays[i].content_id = -1;
748 if (!machine_is(powermac))
751 macio_register_driver(&media_bay_driver);
756 device_initcall(media_bay_init);