// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for the media bay on the PowerBook 3400 and 2400. * * Copyright (C) 1998 Paul Mackerras. * * Various evolutions by Benjamin Herrenschmidt & Henry Worth
*/ #include <linux/types.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/sched.h> #include <linux/timer.h> #include <linux/stddef.h> #include <linux/init.h> #include <linux/kthread.h> #include <linux/mutex.h> #include <linux/pgtable.h>
struct media_bay_info {
u32 __iomem *base; int content_id; int state; int last_value; int value_count; int timer; struct macio_dev *mdev; conststruct mb_ops* ops; int index; int cached_gpio; int sleeping; int user_lock; struct mutex lock;
};
/* * Wait that number of ms between each step in normal polling mode
*/ #define MB_POLL_DELAY 25
/* * Consider the media-bay ID value stable if it is the same for * this number of milliseconds
*/ #define MB_STABLE_DELAY 100
/* Wait after powering up the media bay this delay in ms * timeout bumped for some powerbooks
*/ #define MB_POWER_DELAY 200
/* * Hold the media-bay reset signal true for this many ticks * after a device is inserted before releasing it.
*/ #define MB_RESET_DELAY 50
/* * Wait this long after the reset signal is released and before doing * further operations. After this delay, the IDE reset signal is released * too for an IDE device
*/ #define MB_SETUP_DELAY 100
/* * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted * (or until the device is ready) before calling into the driver
*/ #define MB_IDE_WAIT 1000
/* * States of a media bay
*/ enum {
mb_empty = 0, /* Idle */
mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
mb_up, /* Media bay full */
mb_powering_down /* Powering down (avoid too fast down/up) */
};
staticinlinevoid set_mb_power(struct media_bay_info* bay, int onoff)
{ /* Power up up and assert the bay reset line */ if (onoff) {
bay->ops->power(bay, 1);
bay->state = mb_powering_up;
pr_debug("mediabay%d: powering up\n", bay->index);
} else { /* Make sure everything is powered down & disabled */
bay->ops->power(bay, 0);
bay->state = mb_powering_down;
pr_debug("mediabay%d: powering down\n", bay->index);
}
bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
}
staticvoid poll_media_bay(struct media_bay_info* bay)
{ int id = bay->ops->content(bay);
staticchar *mb_content_types[] = { "a floppy drive", "a floppy drive", "an unsupported audio device", "an ATA device", "an unsupported PCI device", "an unknown device",
};
if (id != bay->last_value) {
bay->last_value = id;
bay->value_count = 0; return;
} if (id == bay->content_id) return;
bay->value_count += msecs_to_jiffies(MB_POLL_DELAY); if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) { /* If the device type changes without going thru * "MB_NO", we force a pass by "MB_NO" to make sure * things are properly reset
*/ if ((id != MB_NO) && (bay->content_id != MB_NO)) {
id = MB_NO;
pr_debug("mediabay%d: forcing MB_NO\n", bay->index);
}
pr_debug("mediabay%d: switching to %d\n", bay->index, id);
set_mb_power(bay, id != MB_NO);
bay->content_id = id; if (id >= MB_NO || id < 0)
printk(KERN_INFO "mediabay%d: Bay is now empty\n", bay->index); else
printk(KERN_INFO "mediabay%d: Bay contains %s\n",
bay->index, mb_content_types[id]);
}
}
int check_media_bay(struct macio_dev *baydev)
{ struct media_bay_info* bay; int id;
if (baydev == NULL) return MB_NO;
/* This returns an instant snapshot, not locking, sine * we may be called with the bay lock held. The resulting * fuzzyness of the result if called at the wrong time is * not actually a huge deal
*/
bay = macio_get_drvdata(baydev); if (bay == NULL) return MB_NO;
id = bay->content_id; if (bay->state != mb_up) return MB_NO; if (id == MB_FD1) return MB_FD; return id;
}
EXPORT_SYMBOL_GPL(check_media_bay);
if (baydev == NULL) return;
bay = macio_get_drvdata(baydev); if (bay == NULL) return; if (bay->user_lock) {
bay->user_lock = 0;
mutex_unlock(&bay->lock);
}
}
EXPORT_SYMBOL_GPL(unlock_media_bay);
staticint mb_broadcast_hotplug(struct device *dev, void *data)
{ struct media_bay_info* bay = data; struct macio_dev *mdev; struct macio_driver *drv; int state;
if (dev->bus != &macio_bus_type) return 0;
state = bay->state == mb_up ? bay->content_id : MB_NO; if (state == MB_FD1)
state = MB_FD;
mdev = to_macio_device(dev);
drv = to_macio_driver(dev->driver); if (dev->driver && drv->mediabay_event)
drv->mediabay_event(mdev, state); return 0;
}
staticvoid media_bay_step(int i)
{ struct media_bay_info* bay = &media_bays[i];
/* We don't poll when powering down */ if (bay->state != mb_powering_down)
poll_media_bay(bay);
/* If timer expired run state machine */ if (bay->timer != 0) {
bay->timer -= msecs_to_jiffies(MB_POLL_DELAY); if (bay->timer > 0) return;
bay->timer = 0;
}
switch(bay->state) { case mb_powering_up: if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
pr_debug("mediabay%d: device not supported (kind:%d)\n",
i, bay->content_id);
set_mb_power(bay, 0); break;
}
bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
bay->state = mb_enabling_bay;
pr_debug("mediabay%d: enabling (kind:%d)\n", i, bay->content_id); break; case mb_enabling_bay:
bay->ops->un_reset(bay);
bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
bay->state = mb_resetting;
pr_debug("mediabay%d: releasing bay reset (kind:%d)\n",
i, bay->content_id); break; case mb_resetting: if (bay->content_id != MB_CD) {
pr_debug("mediabay%d: bay is up (kind:%d)\n", i,
bay->content_id);
bay->state = mb_up;
device_for_each_child(&bay->mdev->ofdev.dev,
bay, mb_broadcast_hotplug); break;
}
pr_debug("mediabay%d: releasing ATA reset (kind:%d)\n",
i, bay->content_id);
bay->ops->un_reset_ide(bay);
bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
bay->state = mb_ide_resetting; break;
case mb_ide_resetting:
pr_debug("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
bay->state = mb_up;
device_for_each_child(&bay->mdev->ofdev.dev,
bay, mb_broadcast_hotplug); break;
case mb_powering_down:
bay->state = mb_empty;
device_for_each_child(&bay->mdev->ofdev.dev,
bay, mb_broadcast_hotplug);
pr_debug("mediabay%d: end of power down\n", i); break;
}
}
/* * This procedure runs as a kernel thread to poll the media bay * once each tick and register and unregister the IDE interface * with the IDE driver. It needs to be a thread because * ide_register can't be called from interrupt context.
*/ staticint media_bay_task(void *x)
{ int i;
while (!kthread_should_stop()) { for (i = 0; i < media_bay_count; ++i) {
mutex_lock(&media_bays[i].lock); if (!media_bays[i].sleeping)
media_bay_step(i);
mutex_unlock(&media_bays[i].lock);
}
if (macio_resource_count(mdev) < 1) return -ENODEV; if (macio_request_resources(mdev, "media-bay")) return -EBUSY; /* Media bay registers are located at the beginning of the * mac-io chip, for now, we trick and align down the first * resource passed in
*/
base = macio_resource_start(mdev, 0) & 0xffff0000u;
regbase = (u32 __iomem *)ioremap(base, 0x100); if (regbase == NULL) {
macio_release_resources(mdev); return -ENOMEM;
}
if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
mdev->ofdev.dev.power.power_state = PMSG_ON;
/* We re-enable the bay using it's previous content only if it did not change. Note those bozo timings, they seem to help the 3400 get it right.
*/ /* Force MB power to 0 */
mutex_lock(&bay->lock);
set_mb_power(bay, 0);
msleep(MB_POWER_DELAY); if (bay->ops->content(bay) != bay->content_id) {
printk("mediabay%d: Content changed during sleep...\n", bay->index);
mutex_unlock(&bay->lock); return 0;
}
set_mb_power(bay, 1);
bay->last_value = bay->content_id;
bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
bay->timer = msecs_to_jiffies(MB_POWER_DELAY); do {
msleep(MB_POLL_DELAY);
media_bay_step(bay->index);
} while((bay->state != mb_empty) &&
(bay->state != mb_up));
bay->sleeping = 0;
mutex_unlock(&bay->lock);
} return 0;
}
/* * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL * register is always set when there is something in the media bay. * This causes problems for the interrupt code if we attach an interrupt * handler to the media-bay interrupt, because it tends to go into * an infinite loop calling the media bay interrupt handler. * Therefore we do it all by polling the media bay once each tick.
*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.