1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the media bay on the PowerBook 3400 and 2400.
5 * Copyright (C) 1998 Paul Mackerras.
7 * Various evolutions by Benjamin Herrenschmidt & Henry Worth
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/sched.h>
14 #include <linux/timer.h>
15 #include <linux/stddef.h>
16 #include <linux/init.h>
17 #include <linux/kthread.h>
18 #include <linux/mutex.h>
19 #include <linux/pgtable.h>
22 #include <asm/machdep.h>
23 #include <asm/pmac_feature.h>
24 #include <asm/mediabay.h>
25 #include <asm/sections.h>
26 #include <asm/ohare.h>
27 #include <asm/heathrow.h>
28 #include <asm/keylargo.h>
29 #include <linux/adb.h>
30 #include <linux/pmu.h>
32 #define MB_FCR32(bay, r) ((bay)->base + ((r) >> 2))
33 #define MB_FCR8(bay, r) (((volatile u8 __iomem *)((bay)->base)) + (r))
35 #define MB_IN32(bay,r) (in_le32(MB_FCR32(bay,r)))
36 #define MB_OUT32(bay,r,v) (out_le32(MB_FCR32(bay,r), (v)))
37 #define MB_BIS(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
38 #define MB_BIC(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
39 #define MB_IN8(bay,r) (in_8(MB_FCR8(bay,r)))
40 #define MB_OUT8(bay,r,v) (out_8(MB_FCR8(bay,r), (v)))
42 struct media_bay_info;
46 void (*init)(struct media_bay_info *bay);
47 u8 (*content)(struct media_bay_info *bay);
48 void (*power)(struct media_bay_info *bay, int on_off);
49 int (*setup_bus)(struct media_bay_info *bay, u8 device_id);
50 void (*un_reset)(struct media_bay_info *bay);
51 void (*un_reset_ide)(struct media_bay_info *bay);
54 struct media_bay_info {
61 struct macio_dev *mdev;
62 const struct mb_ops* ops;
72 static struct media_bay_info media_bays[MAX_BAYS];
73 static int media_bay_count = 0;
76 * Wait that number of ms between each step in normal polling mode
78 #define MB_POLL_DELAY 25
81 * Consider the media-bay ID value stable if it is the same for
82 * this number of milliseconds
84 #define MB_STABLE_DELAY 100
86 /* Wait after powering up the media bay this delay in ms
87 * timeout bumped for some powerbooks
89 #define MB_POWER_DELAY 200
92 * Hold the media-bay reset signal true for this many ticks
93 * after a device is inserted before releasing it.
95 #define MB_RESET_DELAY 50
98 * Wait this long after the reset signal is released and before doing
99 * further operations. After this delay, the IDE reset signal is released
100 * too for an IDE device
102 #define MB_SETUP_DELAY 100
105 * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
106 * (or until the device is ready) before calling into the driver
108 #define MB_IDE_WAIT 1000
111 * States of a media bay
114 mb_empty = 0, /* Idle */
115 mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
116 mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
117 mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
118 mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
119 mb_up, /* Media bay full */
120 mb_powering_down /* Powering down (avoid too fast down/up) */
123 #define MB_POWER_SOUND 0x08
124 #define MB_POWER_FLOPPY 0x04
125 #define MB_POWER_ATA 0x02
126 #define MB_POWER_PCI 0x01
127 #define MB_POWER_OFF 0x00
130 * Functions for polling content of media bay
134 ohare_mb_content(struct media_bay_info *bay)
136 return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
140 heathrow_mb_content(struct media_bay_info *bay)
142 return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
146 keylargo_mb_content(struct media_bay_info *bay)
150 new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
152 bay->cached_gpio = new_gpio;
154 } else if (bay->cached_gpio != new_gpio) {
155 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
156 (void)MB_IN32(bay, KEYLARGO_MBCR);
158 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
159 (void)MB_IN32(bay, KEYLARGO_MBCR);
161 bay->cached_gpio = new_gpio;
163 return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
167 * Functions for powering up/down the bay, puts the bay device
168 * into reset state as well
172 ohare_mb_power(struct media_bay_info* bay, int on_off)
175 /* Power up device, assert it's reset line */
176 MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
177 MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
179 /* Disable all devices */
180 MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
181 MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
182 /* Cut power from bay, release reset line */
183 MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
184 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
185 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
187 MB_BIC(bay, OHARE_MBCR, 0x00000F00);
191 heathrow_mb_power(struct media_bay_info* bay, int on_off)
194 /* Power up device, assert it's reset line */
195 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
196 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
198 /* Disable all devices */
199 MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
200 MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
201 /* Cut power from bay, release reset line */
202 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
203 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
204 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
206 MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
210 keylargo_mb_power(struct media_bay_info* bay, int on_off)
213 /* Power up device, assert it's reset line */
214 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
215 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
217 /* Disable all devices */
218 MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
219 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
220 /* Cut power from bay, release reset line */
221 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
222 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
223 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
225 MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
229 * Functions for configuring the media bay for a given type of device,
230 * enable the related busses
234 ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
239 MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
240 MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
243 MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
244 MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
247 MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
254 heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
259 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
260 MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
263 MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
264 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
267 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
274 keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
278 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
279 MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
280 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
283 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
286 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
293 * Functions for tweaking resets
297 ohare_mb_un_reset(struct media_bay_info* bay)
299 MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
302 static void keylargo_mb_init(struct media_bay_info *bay)
304 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
307 static void heathrow_mb_un_reset(struct media_bay_info* bay)
309 MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
312 static void keylargo_mb_un_reset(struct media_bay_info* bay)
314 MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
317 static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
319 MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
322 static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
324 MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
327 static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
329 MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
332 static inline void set_mb_power(struct media_bay_info* bay, int onoff)
334 /* Power up up and assert the bay reset line */
336 bay->ops->power(bay, 1);
337 bay->state = mb_powering_up;
338 pr_debug("mediabay%d: powering up\n", bay->index);
340 /* Make sure everything is powered down & disabled */
341 bay->ops->power(bay, 0);
342 bay->state = mb_powering_down;
343 pr_debug("mediabay%d: powering down\n", bay->index);
345 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
348 static void poll_media_bay(struct media_bay_info* bay)
350 int id = bay->ops->content(bay);
352 static char *mb_content_types[] = {
355 "an unsupported audio device",
357 "an unsupported PCI device",
361 if (id != bay->last_value) {
362 bay->last_value = id;
363 bay->value_count = 0;
366 if (id == bay->content_id)
369 bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
370 if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
371 /* If the device type changes without going thru
372 * "MB_NO", we force a pass by "MB_NO" to make sure
373 * things are properly reset
375 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
377 pr_debug("mediabay%d: forcing MB_NO\n", bay->index);
379 pr_debug("mediabay%d: switching to %d\n", bay->index, id);
380 set_mb_power(bay, id != MB_NO);
381 bay->content_id = id;
382 if (id >= MB_NO || id < 0)
383 printk(KERN_INFO "mediabay%d: Bay is now empty\n", bay->index);
385 printk(KERN_INFO "mediabay%d: Bay contains %s\n",
386 bay->index, mb_content_types[id]);
390 int check_media_bay(struct macio_dev *baydev)
392 struct media_bay_info* bay;
398 /* This returns an instant snapshot, not locking, sine
399 * we may be called with the bay lock held. The resulting
400 * fuzzyness of the result if called at the wrong time is
401 * not actually a huge deal
403 bay = macio_get_drvdata(baydev);
406 id = bay->content_id;
407 if (bay->state != mb_up)
413 EXPORT_SYMBOL_GPL(check_media_bay);
415 void lock_media_bay(struct macio_dev *baydev)
417 struct media_bay_info* bay;
421 bay = macio_get_drvdata(baydev);
424 mutex_lock(&bay->lock);
427 EXPORT_SYMBOL_GPL(lock_media_bay);
429 void unlock_media_bay(struct macio_dev *baydev)
431 struct media_bay_info* bay;
435 bay = macio_get_drvdata(baydev);
438 if (bay->user_lock) {
440 mutex_unlock(&bay->lock);
443 EXPORT_SYMBOL_GPL(unlock_media_bay);
445 static int mb_broadcast_hotplug(struct device *dev, void *data)
447 struct media_bay_info* bay = data;
448 struct macio_dev *mdev;
449 struct macio_driver *drv;
452 if (dev->bus != &macio_bus_type)
455 state = bay->state == mb_up ? bay->content_id : MB_NO;
458 mdev = to_macio_device(dev);
459 drv = to_macio_driver(dev->driver);
460 if (dev->driver && drv->mediabay_event)
461 drv->mediabay_event(mdev, state);
465 static void media_bay_step(int i)
467 struct media_bay_info* bay = &media_bays[i];
469 /* We don't poll when powering down */
470 if (bay->state != mb_powering_down)
473 /* If timer expired run state machine */
474 if (bay->timer != 0) {
475 bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
483 if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
484 pr_debug("mediabay%d: device not supported (kind:%d)\n",
486 set_mb_power(bay, 0);
489 bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
490 bay->state = mb_enabling_bay;
491 pr_debug("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
493 case mb_enabling_bay:
494 bay->ops->un_reset(bay);
495 bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
496 bay->state = mb_resetting;
497 pr_debug("mediabay%d: releasing bay reset (kind:%d)\n",
501 if (bay->content_id != MB_CD) {
502 pr_debug("mediabay%d: bay is up (kind:%d)\n", i,
505 device_for_each_child(&bay->mdev->ofdev.dev,
506 bay, mb_broadcast_hotplug);
509 pr_debug("mediabay%d: releasing ATA reset (kind:%d)\n",
511 bay->ops->un_reset_ide(bay);
512 bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
513 bay->state = mb_ide_resetting;
516 case mb_ide_resetting:
517 pr_debug("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
519 device_for_each_child(&bay->mdev->ofdev.dev,
520 bay, mb_broadcast_hotplug);
523 case mb_powering_down:
524 bay->state = mb_empty;
525 device_for_each_child(&bay->mdev->ofdev.dev,
526 bay, mb_broadcast_hotplug);
527 pr_debug("mediabay%d: end of power down\n", i);
533 * This procedure runs as a kernel thread to poll the media bay
534 * once each tick and register and unregister the IDE interface
535 * with the IDE driver. It needs to be a thread because
536 * ide_register can't be called from interrupt context.
538 static int media_bay_task(void *x)
542 while (!kthread_should_stop()) {
543 for (i = 0; i < media_bay_count; ++i) {
544 mutex_lock(&media_bays[i].lock);
545 if (!media_bays[i].sleeping)
547 mutex_unlock(&media_bays[i].lock);
550 msleep_interruptible(MB_POLL_DELAY);
555 static int media_bay_attach(struct macio_dev *mdev,
556 const struct of_device_id *match)
558 struct media_bay_info* bay;
559 u32 __iomem *regbase;
560 struct device_node *ofnode;
564 ofnode = mdev->ofdev.dev.of_node;
566 if (macio_resource_count(mdev) < 1)
568 if (macio_request_resources(mdev, "media-bay"))
570 /* Media bay registers are located at the beginning of the
571 * mac-io chip, for now, we trick and align down the first
574 base = macio_resource_start(mdev, 0) & 0xffff0000u;
575 regbase = (u32 __iomem *)ioremap(base, 0x100);
576 if (regbase == NULL) {
577 macio_release_resources(mdev);
581 i = media_bay_count++;
582 bay = &media_bays[i];
586 bay->ops = match->data;
588 mutex_init(&bay->lock);
590 /* Init HW probing */
594 printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
596 /* Force an immediate detect */
597 set_mb_power(bay, 0);
598 msleep(MB_POWER_DELAY);
599 bay->content_id = MB_NO;
600 bay->last_value = bay->ops->content(bay);
601 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
602 bay->state = mb_empty;
604 /* Mark us ready by filling our mdev data */
605 macio_set_drvdata(mdev, bay);
607 /* Startup kernel thread */
609 kthread_run(media_bay_task, NULL, "media-bay");
615 static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
617 struct media_bay_info *bay = macio_get_drvdata(mdev);
619 if (state.event != mdev->ofdev.dev.power.power_state.event
620 && (state.event & PM_EVENT_SLEEP)) {
621 mutex_lock(&bay->lock);
623 set_mb_power(bay, 0);
624 mutex_unlock(&bay->lock);
625 msleep(MB_POLL_DELAY);
626 mdev->ofdev.dev.power.power_state = state;
631 static int media_bay_resume(struct macio_dev *mdev)
633 struct media_bay_info *bay = macio_get_drvdata(mdev);
635 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
636 mdev->ofdev.dev.power.power_state = PMSG_ON;
638 /* We re-enable the bay using it's previous content
639 only if it did not change. Note those bozo timings,
640 they seem to help the 3400 get it right.
642 /* Force MB power to 0 */
643 mutex_lock(&bay->lock);
644 set_mb_power(bay, 0);
645 msleep(MB_POWER_DELAY);
646 if (bay->ops->content(bay) != bay->content_id) {
647 printk("mediabay%d: Content changed during sleep...\n", bay->index);
648 mutex_unlock(&bay->lock);
651 set_mb_power(bay, 1);
652 bay->last_value = bay->content_id;
653 bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
654 bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
656 msleep(MB_POLL_DELAY);
657 media_bay_step(bay->index);
658 } while((bay->state != mb_empty) &&
659 (bay->state != mb_up));
661 mutex_unlock(&bay->lock);
667 /* Definitions of "ops" structures.
669 static const struct mb_ops ohare_mb_ops = {
671 .content = ohare_mb_content,
672 .power = ohare_mb_power,
673 .setup_bus = ohare_mb_setup_bus,
674 .un_reset = ohare_mb_un_reset,
675 .un_reset_ide = ohare_mb_un_reset_ide,
678 static const struct mb_ops heathrow_mb_ops = {
680 .content = heathrow_mb_content,
681 .power = heathrow_mb_power,
682 .setup_bus = heathrow_mb_setup_bus,
683 .un_reset = heathrow_mb_un_reset,
684 .un_reset_ide = heathrow_mb_un_reset_ide,
687 static const struct mb_ops keylargo_mb_ops = {
689 .init = keylargo_mb_init,
690 .content = keylargo_mb_content,
691 .power = keylargo_mb_power,
692 .setup_bus = keylargo_mb_setup_bus,
693 .un_reset = keylargo_mb_un_reset,
694 .un_reset_ide = keylargo_mb_un_reset_ide,
698 * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
699 * register is always set when there is something in the media bay.
700 * This causes problems for the interrupt code if we attach an interrupt
701 * handler to the media-bay interrupt, because it tends to go into
702 * an infinite loop calling the media bay interrupt handler.
703 * Therefore we do it all by polling the media bay once each tick.
706 static const struct of_device_id media_bay_match[] =
710 .compatible = "keylargo-media-bay",
711 .data = &keylargo_mb_ops,
715 .compatible = "heathrow-media-bay",
716 .data = &heathrow_mb_ops,
720 .compatible = "ohare-media-bay",
721 .data = &ohare_mb_ops,
726 static struct macio_driver media_bay_driver =
730 .of_match_table = media_bay_match,
732 .probe = media_bay_attach,
733 .suspend = media_bay_suspend,
734 .resume = media_bay_resume
737 static int __init media_bay_init(void)
741 for (i=0; i<MAX_BAYS; i++) {
742 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
743 media_bays[i].content_id = -1;
745 if (!machine_is(powermac))
748 macio_register_driver(&media_bay_driver);
753 device_initcall(media_bay_init);