xref: /openbmc/linux/drivers/macintosh/mediabay.c (revision 9a24729d8aeef967eac7af71c6a69edc83d06558)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Driver for the media bay on the PowerBook 3400 and 2400.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1998 Paul Mackerras.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Various evolutions by Benjamin Herrenschmidt & Henry Worth
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *  modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *  as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *  2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/delay.h>
171da177e4SLinus Torvalds #include <linux/sched.h>
181da177e4SLinus Torvalds #include <linux/timer.h>
191da177e4SLinus Torvalds #include <linux/hdreg.h>
201da177e4SLinus Torvalds #include <linux/stddef.h>
211da177e4SLinus Torvalds #include <linux/init.h>
221da177e4SLinus Torvalds #include <linux/ide.h>
2333f6e794SPaul Mackerras #include <linux/kthread.h>
24*9a24729dSDaniel Walker #include <linux/mutex.h>
251da177e4SLinus Torvalds #include <asm/prom.h>
261da177e4SLinus Torvalds #include <asm/pgtable.h>
271da177e4SLinus Torvalds #include <asm/io.h>
281da177e4SLinus Torvalds #include <asm/machdep.h>
291da177e4SLinus Torvalds #include <asm/pmac_feature.h>
301da177e4SLinus Torvalds #include <asm/mediabay.h>
311da177e4SLinus Torvalds #include <asm/sections.h>
321da177e4SLinus Torvalds #include <asm/ohare.h>
331da177e4SLinus Torvalds #include <asm/heathrow.h>
341da177e4SLinus Torvalds #include <asm/keylargo.h>
351da177e4SLinus Torvalds #include <linux/adb.h>
361da177e4SLinus Torvalds #include <linux/pmu.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define MB_DEBUG
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #ifdef MB_DEBUG
421da177e4SLinus Torvalds #define MBDBG(fmt, arg...)	printk(KERN_INFO fmt , ## arg)
431da177e4SLinus Torvalds #else
441da177e4SLinus Torvalds #define MBDBG(fmt, arg...)	do { } while (0)
451da177e4SLinus Torvalds #endif
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define MB_FCR32(bay, r)	((bay)->base + ((r) >> 2))
481da177e4SLinus Torvalds #define MB_FCR8(bay, r)		(((volatile u8 __iomem *)((bay)->base)) + (r))
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define MB_IN32(bay,r)		(in_le32(MB_FCR32(bay,r)))
511da177e4SLinus Torvalds #define MB_OUT32(bay,r,v)	(out_le32(MB_FCR32(bay,r), (v)))
521da177e4SLinus Torvalds #define MB_BIS(bay,r,v)		(MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
531da177e4SLinus Torvalds #define MB_BIC(bay,r,v)		(MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
541da177e4SLinus Torvalds #define MB_IN8(bay,r)		(in_8(MB_FCR8(bay,r)))
551da177e4SLinus Torvalds #define MB_OUT8(bay,r,v)	(out_8(MB_FCR8(bay,r), (v)))
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds struct media_bay_info;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds struct mb_ops {
601da177e4SLinus Torvalds 	char*	name;
611da177e4SLinus Torvalds 	void	(*init)(struct media_bay_info *bay);
621da177e4SLinus Torvalds 	u8	(*content)(struct media_bay_info *bay);
631da177e4SLinus Torvalds 	void	(*power)(struct media_bay_info *bay, int on_off);
641da177e4SLinus Torvalds 	int	(*setup_bus)(struct media_bay_info *bay, u8 device_id);
651da177e4SLinus Torvalds 	void	(*un_reset)(struct media_bay_info *bay);
661da177e4SLinus Torvalds 	void	(*un_reset_ide)(struct media_bay_info *bay);
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds struct media_bay_info {
701da177e4SLinus Torvalds 	u32 __iomem			*base;
711da177e4SLinus Torvalds 	int				content_id;
721da177e4SLinus Torvalds 	int				state;
731da177e4SLinus Torvalds 	int				last_value;
741da177e4SLinus Torvalds 	int				value_count;
751da177e4SLinus Torvalds 	int				timer;
761da177e4SLinus Torvalds 	struct macio_dev		*mdev;
771da177e4SLinus Torvalds 	struct mb_ops*			ops;
781da177e4SLinus Torvalds 	int				index;
791da177e4SLinus Torvalds 	int				cached_gpio;
801da177e4SLinus Torvalds 	int				sleeping;
81*9a24729dSDaniel Walker 	struct mutex			lock;
8234394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
832dde7861SBartlomiej Zolnierkiewicz 	ide_hwif_t			*cd_port;
841da177e4SLinus Torvalds 	void __iomem			*cd_base;
851da177e4SLinus Torvalds 	int				cd_irq;
861da177e4SLinus Torvalds 	int				cd_retry;
871da177e4SLinus Torvalds #endif
88305c7368SAdrian Bunk #if defined(CONFIG_BLK_DEV_IDE_PMAC)
8934394e45SBartlomiej Zolnierkiewicz 	int 				cd_index;
9034394e45SBartlomiej Zolnierkiewicz #endif
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define MAX_BAYS	2
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static struct media_bay_info media_bays[MAX_BAYS];
961da177e4SLinus Torvalds int media_bay_count = 0;
971da177e4SLinus Torvalds 
9834394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
991da177e4SLinus Torvalds /* check the busy bit in the media-bay ide interface
1001da177e4SLinus Torvalds    (assumes the media-bay contains an ide device) */
1011da177e4SLinus Torvalds #define MB_IDE_READY(i)	((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
1021da177e4SLinus Torvalds #endif
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Wait that number of ms between each step in normal polling mode
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds #define MB_POLL_DELAY	25
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * Consider the media-bay ID value stable if it is the same for
1111da177e4SLinus Torvalds  * this number of milliseconds
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds #define MB_STABLE_DELAY	100
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* Wait after powering up the media bay this delay in ms
1161da177e4SLinus Torvalds  * timeout bumped for some powerbooks
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds #define MB_POWER_DELAY	200
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /*
1211da177e4SLinus Torvalds  * Hold the media-bay reset signal true for this many ticks
1221da177e4SLinus Torvalds  * after a device is inserted before releasing it.
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds #define MB_RESET_DELAY	50
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  * Wait this long after the reset signal is released and before doing
1281da177e4SLinus Torvalds  * further operations. After this delay, the IDE reset signal is released
1291da177e4SLinus Torvalds  * too for an IDE device
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds #define MB_SETUP_DELAY	100
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
1341da177e4SLinus Torvalds  * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
1351da177e4SLinus Torvalds  * (or until the device is ready) before waiting for busy bit to disappear
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds #define MB_IDE_WAIT	1000
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * Timeout waiting for busy bit of an IDE device to go down
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds #define MB_IDE_TIMEOUT	5000
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * Max retries of the full power up/down sequence for an IDE device
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds #define MAX_CD_RETRIES	3
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  * States of a media bay
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds enum {
1531da177e4SLinus Torvalds 	mb_empty = 0,		/* Idle */
1541da177e4SLinus Torvalds 	mb_powering_up,		/* power bit set, waiting MB_POWER_DELAY */
1551da177e4SLinus Torvalds 	mb_enabling_bay,	/* enable bits set, waiting MB_RESET_DELAY */
1561da177e4SLinus Torvalds 	mb_resetting,		/* reset bit unset, waiting MB_SETUP_DELAY */
1571da177e4SLinus Torvalds 	mb_ide_resetting,	/* IDE reset bit unser, waiting MB_IDE_WAIT */
1581da177e4SLinus Torvalds 	mb_ide_waiting,		/* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
1591da177e4SLinus Torvalds 	mb_up,			/* Media bay full */
1601da177e4SLinus Torvalds 	mb_powering_down	/* Powering down (avoid too fast down/up) */
1611da177e4SLinus Torvalds };
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds #define MB_POWER_SOUND		0x08
1641da177e4SLinus Torvalds #define MB_POWER_FLOPPY		0x04
1651da177e4SLinus Torvalds #define MB_POWER_ATA		0x02
1661da177e4SLinus Torvalds #define MB_POWER_PCI		0x01
1671da177e4SLinus Torvalds #define MB_POWER_OFF		0x00
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
1701da177e4SLinus Torvalds  * Functions for polling content of media bay
1711da177e4SLinus Torvalds  */
1721da177e4SLinus Torvalds 
173aacaf9bdSJon Loeliger static u8
1741da177e4SLinus Torvalds ohare_mb_content(struct media_bay_info *bay)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179aacaf9bdSJon Loeliger static u8
1801da177e4SLinus Torvalds heathrow_mb_content(struct media_bay_info *bay)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
185aacaf9bdSJon Loeliger static u8
1861da177e4SLinus Torvalds keylargo_mb_content(struct media_bay_info *bay)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	int new_gpio;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
1911da177e4SLinus Torvalds 	if (new_gpio) {
1921da177e4SLinus Torvalds 		bay->cached_gpio = new_gpio;
1931da177e4SLinus Torvalds 		return MB_NO;
1941da177e4SLinus Torvalds 	} else if (bay->cached_gpio != new_gpio) {
1951da177e4SLinus Torvalds 		MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
1961da177e4SLinus Torvalds 		(void)MB_IN32(bay, KEYLARGO_MBCR);
1971da177e4SLinus Torvalds 		udelay(5);
1981da177e4SLinus Torvalds 		MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
1991da177e4SLinus Torvalds 		(void)MB_IN32(bay, KEYLARGO_MBCR);
2001da177e4SLinus Torvalds 		udelay(5);
2011da177e4SLinus Torvalds 		bay->cached_gpio = new_gpio;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 	return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds /*
2071da177e4SLinus Torvalds  * Functions for powering up/down the bay, puts the bay device
2081da177e4SLinus Torvalds  * into reset state as well
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds 
211aacaf9bdSJon Loeliger static void
2121da177e4SLinus Torvalds ohare_mb_power(struct media_bay_info* bay, int on_off)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	if (on_off) {
2151da177e4SLinus Torvalds 		/* Power up device, assert it's reset line */
2161da177e4SLinus Torvalds 		MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
2171da177e4SLinus Torvalds 		MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
2181da177e4SLinus Torvalds 	} else {
2191da177e4SLinus Torvalds 		/* Disable all devices */
2201da177e4SLinus Torvalds 		MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
2211da177e4SLinus Torvalds 		MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
2221da177e4SLinus Torvalds 		/* Cut power from bay, release reset line */
2231da177e4SLinus Torvalds 		MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
2241da177e4SLinus Torvalds 		MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
2251da177e4SLinus Torvalds 		MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	MB_BIC(bay, OHARE_MBCR, 0x00000F00);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230aacaf9bdSJon Loeliger static void
2311da177e4SLinus Torvalds heathrow_mb_power(struct media_bay_info* bay, int on_off)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	if (on_off) {
2341da177e4SLinus Torvalds 		/* Power up device, assert it's reset line */
2351da177e4SLinus Torvalds 		MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
2361da177e4SLinus Torvalds 		MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
2371da177e4SLinus Torvalds 	} else {
2381da177e4SLinus Torvalds 		/* Disable all devices */
2391da177e4SLinus Torvalds 		MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
2401da177e4SLinus Torvalds 		MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
2411da177e4SLinus Torvalds 		/* Cut power from bay, release reset line */
2421da177e4SLinus Torvalds 		MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
2431da177e4SLinus Torvalds 		MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
2441da177e4SLinus Torvalds 		MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 	MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249aacaf9bdSJon Loeliger static void
2501da177e4SLinus Torvalds keylargo_mb_power(struct media_bay_info* bay, int on_off)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	if (on_off) {
2531da177e4SLinus Torvalds 		/* Power up device, assert it's reset line */
2541da177e4SLinus Torvalds             	MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
2551da177e4SLinus Torvalds             	MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
2561da177e4SLinus Torvalds 	} else {
2571da177e4SLinus Torvalds 		/* Disable all devices */
2581da177e4SLinus Torvalds 		MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
2591da177e4SLinus Torvalds 		MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
2601da177e4SLinus Torvalds 		/* Cut power from bay, release reset line */
2611da177e4SLinus Torvalds 		MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
2621da177e4SLinus Torvalds 		MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
2631da177e4SLinus Torvalds 		MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 	MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /*
2691da177e4SLinus Torvalds  * Functions for configuring the media bay for a given type of device,
2701da177e4SLinus Torvalds  * enable the related busses
2711da177e4SLinus Torvalds  */
2721da177e4SLinus Torvalds 
273aacaf9bdSJon Loeliger static int
2741da177e4SLinus Torvalds ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	switch(device_id) {
2771da177e4SLinus Torvalds 		case MB_FD:
2781da177e4SLinus Torvalds 		case MB_FD1:
2791da177e4SLinus Torvalds 			MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
2801da177e4SLinus Torvalds 			MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
2811da177e4SLinus Torvalds 			return 0;
2821da177e4SLinus Torvalds 		case MB_CD:
2831da177e4SLinus Torvalds 			MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
2841da177e4SLinus Torvalds 			MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
2851da177e4SLinus Torvalds 			return 0;
2861da177e4SLinus Torvalds 		case MB_PCI:
2871da177e4SLinus Torvalds 			MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
2881da177e4SLinus Torvalds 			return 0;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	return -ENODEV;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
293aacaf9bdSJon Loeliger static int
2941da177e4SLinus Torvalds heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	switch(device_id) {
2971da177e4SLinus Torvalds 		case MB_FD:
2981da177e4SLinus Torvalds 		case MB_FD1:
2991da177e4SLinus Torvalds 			MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
3001da177e4SLinus Torvalds 			MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
3011da177e4SLinus Torvalds 			return 0;
3021da177e4SLinus Torvalds 		case MB_CD:
3031da177e4SLinus Torvalds 			MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
3041da177e4SLinus Torvalds 			MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
3051da177e4SLinus Torvalds 			return 0;
3061da177e4SLinus Torvalds 		case MB_PCI:
3071da177e4SLinus Torvalds 			MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
3081da177e4SLinus Torvalds 			return 0;
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds 	return -ENODEV;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
313aacaf9bdSJon Loeliger static int
3141da177e4SLinus Torvalds keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	switch(device_id) {
3171da177e4SLinus Torvalds 		case MB_CD:
3181da177e4SLinus Torvalds 			MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
3191da177e4SLinus Torvalds 			MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
3201da177e4SLinus Torvalds 			MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
3211da177e4SLinus Torvalds 			return 0;
3221da177e4SLinus Torvalds 		case MB_PCI:
3231da177e4SLinus Torvalds 			MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
3241da177e4SLinus Torvalds 			return 0;
3251da177e4SLinus Torvalds 		case MB_SOUND:
3261da177e4SLinus Torvalds 			MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
3271da177e4SLinus Torvalds 			return 0;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	return -ENODEV;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  * Functions for tweaking resets
3341da177e4SLinus Torvalds  */
3351da177e4SLinus Torvalds 
336aacaf9bdSJon Loeliger static void
3371da177e4SLinus Torvalds ohare_mb_un_reset(struct media_bay_info* bay)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
342aacaf9bdSJon Loeliger static void keylargo_mb_init(struct media_bay_info *bay)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
347aacaf9bdSJon Loeliger static void heathrow_mb_un_reset(struct media_bay_info* bay)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352aacaf9bdSJon Loeliger static void keylargo_mb_un_reset(struct media_bay_info* bay)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
357aacaf9bdSJon Loeliger static void ohare_mb_un_reset_ide(struct media_bay_info* bay)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362aacaf9bdSJon Loeliger static void heathrow_mb_un_reset_ide(struct media_bay_info* bay)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
367aacaf9bdSJon Loeliger static void keylargo_mb_un_reset_ide(struct media_bay_info* bay)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
372aacaf9bdSJon Loeliger static inline void set_mb_power(struct media_bay_info* bay, int onoff)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	/* Power up up and assert the bay reset line */
3751da177e4SLinus Torvalds 	if (onoff) {
3761da177e4SLinus Torvalds 		bay->ops->power(bay, 1);
3771da177e4SLinus Torvalds 		bay->state = mb_powering_up;
3781da177e4SLinus Torvalds 		MBDBG("mediabay%d: powering up\n", bay->index);
3791da177e4SLinus Torvalds 	} else {
3801da177e4SLinus Torvalds 		/* Make sure everything is powered down & disabled */
3811da177e4SLinus Torvalds 		bay->ops->power(bay, 0);
3821da177e4SLinus Torvalds 		bay->state = mb_powering_down;
3831da177e4SLinus Torvalds 		MBDBG("mediabay%d: powering down\n", bay->index);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 	bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
388aacaf9bdSJon Loeliger static void poll_media_bay(struct media_bay_info* bay)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	int id = bay->ops->content(bay);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	if (id == bay->last_value) {
3931da177e4SLinus Torvalds 		if (id != bay->content_id) {
3941da177e4SLinus Torvalds 			bay->value_count += msecs_to_jiffies(MB_POLL_DELAY);
3951da177e4SLinus Torvalds 			if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) {
3961da177e4SLinus Torvalds 				/* If the device type changes without going thru
3971da177e4SLinus Torvalds 				 * "MB_NO", we force a pass by "MB_NO" to make sure
3981da177e4SLinus Torvalds 				 * things are properly reset
3991da177e4SLinus Torvalds 				 */
4001da177e4SLinus Torvalds 				if ((id != MB_NO) && (bay->content_id != MB_NO)) {
4011da177e4SLinus Torvalds 					id = MB_NO;
4021da177e4SLinus Torvalds 					MBDBG("mediabay%d: forcing MB_NO\n", bay->index);
4031da177e4SLinus Torvalds 				}
4041da177e4SLinus Torvalds 				MBDBG("mediabay%d: switching to %d\n", bay->index, id);
4051da177e4SLinus Torvalds 				set_mb_power(bay, id != MB_NO);
4061da177e4SLinus Torvalds 				bay->content_id = id;
4071da177e4SLinus Torvalds 				if (id == MB_NO) {
40834394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
4091da177e4SLinus Torvalds 					bay->cd_retry = 0;
4101da177e4SLinus Torvalds #endif
4111da177e4SLinus Torvalds 					printk(KERN_INFO "media bay %d is empty\n", bay->index);
4121da177e4SLinus Torvalds 				}
4131da177e4SLinus Torvalds 			}
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 	} else {
4161da177e4SLinus Torvalds 		bay->last_value = id;
4171da177e4SLinus Torvalds 		bay->value_count = 0;
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
421305c7368SAdrian Bunk #ifdef CONFIG_BLK_DEV_IDE_PMAC
422aacaf9bdSJon Loeliger int check_media_bay(struct device_node *which_bay, int what)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	int	i;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	for (i=0; i<media_bay_count; i++)
4271da177e4SLinus Torvalds 		if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) {
4281da177e4SLinus Torvalds 			if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
4291da177e4SLinus Torvalds 				return 0;
4301da177e4SLinus Torvalds 			media_bays[i].cd_index = -1;
4311da177e4SLinus Torvalds 			return -EINVAL;
4321da177e4SLinus Torvalds 		}
4331da177e4SLinus Torvalds 	return -ENODEV;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds EXPORT_SYMBOL(check_media_bay);
4361da177e4SLinus Torvalds 
437aacaf9bdSJon Loeliger int check_media_bay_by_base(unsigned long base, int what)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	int	i;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	for (i=0; i<media_bay_count; i++)
4421da177e4SLinus Torvalds 		if (media_bays[i].mdev && base == (unsigned long) media_bays[i].cd_base) {
4431da177e4SLinus Torvalds 			if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
4441da177e4SLinus Torvalds 				return 0;
4451da177e4SLinus Torvalds 			media_bays[i].cd_index = -1;
4461da177e4SLinus Torvalds 			return -EINVAL;
4471da177e4SLinus Torvalds 		}
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	return -ENODEV;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
452aacaf9bdSJon Loeliger int media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
4532dde7861SBartlomiej Zolnierkiewicz 			    int irq, ide_hwif_t *hwif)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	int	i;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	for (i=0; i<media_bay_count; i++) {
4581da177e4SLinus Torvalds 		struct media_bay_info* bay = &media_bays[i];
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 		if (bay->mdev && which_bay == bay->mdev->ofdev.node) {
4612dde7861SBartlomiej Zolnierkiewicz 			int timeout = 5000, index = hwif->index;
4621da177e4SLinus Torvalds 
463*9a24729dSDaniel Walker 			mutex_lock(&bay->lock);
4641da177e4SLinus Torvalds 
4652dde7861SBartlomiej Zolnierkiewicz 			bay->cd_port	= hwif;
4661da177e4SLinus Torvalds  			bay->cd_base	= (void __iomem *) base;
4671da177e4SLinus Torvalds 			bay->cd_irq	= irq;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 			if ((MB_CD != bay->content_id) || bay->state != mb_up) {
470*9a24729dSDaniel Walker 				mutex_unlock(&bay->lock);
4711da177e4SLinus Torvalds 				return 0;
4721da177e4SLinus Torvalds 			}
4731da177e4SLinus Torvalds 			printk(KERN_DEBUG "Registered ide%d for media bay %d\n", index, i);
4741da177e4SLinus Torvalds 			do {
4751da177e4SLinus Torvalds 				if (MB_IDE_READY(i)) {
4761da177e4SLinus Torvalds 					bay->cd_index	= index;
477*9a24729dSDaniel Walker 					mutex_unlock(&bay->lock);
4781da177e4SLinus Torvalds 					return 0;
4791da177e4SLinus Torvalds 				}
4801da177e4SLinus Torvalds 				mdelay(1);
4811da177e4SLinus Torvalds 			} while(--timeout);
4821da177e4SLinus Torvalds 			printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i);
483*9a24729dSDaniel Walker 			mutex_unlock(&bay->lock);
4841da177e4SLinus Torvalds 			return -ENODEV;
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	return -ENODEV;
4891da177e4SLinus Torvalds }
49034394e45SBartlomiej Zolnierkiewicz #endif /* CONFIG_BLK_DEV_IDE_PMAC */
4911da177e4SLinus Torvalds 
492aacaf9bdSJon Loeliger static void media_bay_step(int i)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	struct media_bay_info* bay = &media_bays[i];
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/* We don't poll when powering down */
4971da177e4SLinus Torvalds 	if (bay->state != mb_powering_down)
4981da177e4SLinus Torvalds 	    poll_media_bay(bay);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/* If timer expired or polling IDE busy, run state machine */
5011da177e4SLinus Torvalds 	if ((bay->state != mb_ide_waiting) && (bay->timer != 0)) {
5021da177e4SLinus Torvalds 		bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
5031da177e4SLinus Torvalds 		if (bay->timer > 0)
5041da177e4SLinus Torvalds 			return;
5051da177e4SLinus Torvalds 		bay->timer = 0;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	switch(bay->state) {
5091da177e4SLinus Torvalds 	case mb_powering_up:
5101da177e4SLinus Torvalds 	    	if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
5111da177e4SLinus Torvalds 			MBDBG("mediabay%d: device not supported (kind:%d)\n", i, bay->content_id);
5121da177e4SLinus Torvalds 	    		set_mb_power(bay, 0);
5131da177e4SLinus Torvalds 	    		break;
5141da177e4SLinus Torvalds 	    	}
5151da177e4SLinus Torvalds 	    	bay->timer = msecs_to_jiffies(MB_RESET_DELAY);
5161da177e4SLinus Torvalds 	    	bay->state = mb_enabling_bay;
5171da177e4SLinus Torvalds 		MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
5181da177e4SLinus Torvalds 		break;
5191da177e4SLinus Torvalds 	case mb_enabling_bay:
5201da177e4SLinus Torvalds 		bay->ops->un_reset(bay);
5211da177e4SLinus Torvalds 	    	bay->timer = msecs_to_jiffies(MB_SETUP_DELAY);
5221da177e4SLinus Torvalds 	    	bay->state = mb_resetting;
5231da177e4SLinus Torvalds 		MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
5241da177e4SLinus Torvalds 	    	break;
5251da177e4SLinus Torvalds 	case mb_resetting:
5261da177e4SLinus Torvalds 		if (bay->content_id != MB_CD) {
5271da177e4SLinus Torvalds 			MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
5281da177e4SLinus Torvalds 			bay->state = mb_up;
5291da177e4SLinus Torvalds 			break;
5301da177e4SLinus Torvalds 	    	}
53134394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
5321da177e4SLinus Torvalds 		MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
5331da177e4SLinus Torvalds 		bay->ops->un_reset_ide(bay);
5341da177e4SLinus Torvalds 	    	bay->timer = msecs_to_jiffies(MB_IDE_WAIT);
5351da177e4SLinus Torvalds 	    	bay->state = mb_ide_resetting;
5361da177e4SLinus Torvalds #else
5371da177e4SLinus Torvalds 		printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
5381da177e4SLinus Torvalds 		set_mb_power(bay, 0);
53934394e45SBartlomiej Zolnierkiewicz #endif /* CONFIG_BLK_DEV_IDE_PMAC */
5401da177e4SLinus Torvalds 	    	break;
54134394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
5421da177e4SLinus Torvalds 	case mb_ide_resetting:
5431da177e4SLinus Torvalds 	    	bay->timer = msecs_to_jiffies(MB_IDE_TIMEOUT);
5441da177e4SLinus Torvalds 	    	bay->state = mb_ide_waiting;
5451da177e4SLinus Torvalds 		MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
5461da177e4SLinus Torvalds 	    	break;
5471da177e4SLinus Torvalds 	case mb_ide_waiting:
5481da177e4SLinus Torvalds 		if (bay->cd_base == NULL) {
5491da177e4SLinus Torvalds 			bay->timer = 0;
5501da177e4SLinus Torvalds 			bay->state = mb_up;
5511da177e4SLinus Torvalds 			MBDBG("mediabay%d: up before IDE init\n", i);
5521da177e4SLinus Torvalds 			break;
5531da177e4SLinus Torvalds 		} else if (MB_IDE_READY(i)) {
5541da177e4SLinus Torvalds 			bay->timer = 0;
5551da177e4SLinus Torvalds 			bay->state = mb_up;
5561da177e4SLinus Torvalds 			if (bay->cd_index < 0) {
5571da177e4SLinus Torvalds 				printk("mediabay %d, registering IDE...\n", i);
5581da177e4SLinus Torvalds 				pmu_suspend();
5592dde7861SBartlomiej Zolnierkiewicz 				ide_port_scan(bay->cd_port);
560c1a8e398SBartlomiej Zolnierkiewicz 				if (bay->cd_port->present)
5612dde7861SBartlomiej Zolnierkiewicz 					bay->cd_index = bay->cd_port->index;
5621da177e4SLinus Torvalds 				pmu_resume();
5631da177e4SLinus Torvalds 			}
5641da177e4SLinus Torvalds 			if (bay->cd_index == -1) {
5651da177e4SLinus Torvalds 				/* We eventually do a retry */
5661da177e4SLinus Torvalds 				bay->cd_retry++;
5671da177e4SLinus Torvalds 				printk("IDE register error\n");
5681da177e4SLinus Torvalds 				set_mb_power(bay, 0);
5691da177e4SLinus Torvalds 			} else {
5701da177e4SLinus Torvalds 				printk(KERN_DEBUG "media-bay %d is ide%d\n", i, bay->cd_index);
5711da177e4SLinus Torvalds 				MBDBG("mediabay %d IDE ready\n", i);
5721da177e4SLinus Torvalds 			}
5731da177e4SLinus Torvalds 			break;
5741da177e4SLinus Torvalds 	    	} else if (bay->timer > 0)
5751da177e4SLinus Torvalds 			bay->timer -= msecs_to_jiffies(MB_POLL_DELAY);
5761da177e4SLinus Torvalds 	    	if (bay->timer <= 0) {
5771da177e4SLinus Torvalds 			printk("\nIDE Timeout in bay %d !, IDE state is: 0x%02x\n",
5781da177e4SLinus Torvalds 			       i, readb(bay->cd_base + 0x70));
5791da177e4SLinus Torvalds 			MBDBG("mediabay%d: nIDE Timeout !\n", i);
5801da177e4SLinus Torvalds 			set_mb_power(bay, 0);
5811da177e4SLinus Torvalds 			bay->timer = 0;
5821da177e4SLinus Torvalds 	    	}
5831da177e4SLinus Torvalds 		break;
58434394e45SBartlomiej Zolnierkiewicz #endif /* CONFIG_BLK_DEV_IDE_PMAC */
5851da177e4SLinus Torvalds 	case mb_powering_down:
5861da177e4SLinus Torvalds 	    	bay->state = mb_empty;
58734394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
5881da177e4SLinus Torvalds     	        if (bay->cd_index >= 0) {
5891da177e4SLinus Torvalds 			printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
5901da177e4SLinus Torvalds 			       bay->cd_index);
5912dde7861SBartlomiej Zolnierkiewicz 			ide_port_unregister_devices(bay->cd_port);
5921da177e4SLinus Torvalds 			bay->cd_index = -1;
5931da177e4SLinus Torvalds 		}
5941da177e4SLinus Torvalds 	    	if (bay->cd_retry) {
5951da177e4SLinus Torvalds 			if (bay->cd_retry > MAX_CD_RETRIES) {
5961da177e4SLinus Torvalds 				/* Should add an error sound (sort of beep in dmasound) */
5971da177e4SLinus Torvalds 				printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
5981da177e4SLinus Torvalds 			} else {
5991da177e4SLinus Torvalds 				/* Force a new power down/up sequence */
6001da177e4SLinus Torvalds 				bay->content_id = MB_NO;
6011da177e4SLinus Torvalds 			}
6021da177e4SLinus Torvalds 	    	}
60334394e45SBartlomiej Zolnierkiewicz #endif /* CONFIG_BLK_DEV_IDE_PMAC */
6041da177e4SLinus Torvalds 		MBDBG("mediabay%d: end of power down\n", i);
6051da177e4SLinus Torvalds 	    	break;
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds /*
6101da177e4SLinus Torvalds  * This procedure runs as a kernel thread to poll the media bay
6111da177e4SLinus Torvalds  * once each tick and register and unregister the IDE interface
6121da177e4SLinus Torvalds  * with the IDE driver.  It needs to be a thread because
6131da177e4SLinus Torvalds  * ide_register can't be called from interrupt context.
6141da177e4SLinus Torvalds  */
615aacaf9bdSJon Loeliger static int media_bay_task(void *x)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	int	i;
6181da177e4SLinus Torvalds 
61933f6e794SPaul Mackerras 	while (!kthread_should_stop()) {
6201da177e4SLinus Torvalds 		for (i = 0; i < media_bay_count; ++i) {
621*9a24729dSDaniel Walker 			mutex_lock(&media_bays[i].lock);
6221da177e4SLinus Torvalds 			if (!media_bays[i].sleeping)
6231da177e4SLinus Torvalds 				media_bay_step(i);
624*9a24729dSDaniel Walker 			mutex_unlock(&media_bays[i].lock);
6251da177e4SLinus Torvalds 		}
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 		msleep_interruptible(MB_POLL_DELAY);
6281da177e4SLinus Torvalds 	}
62933f6e794SPaul Mackerras 	return 0;
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6325e655772SJeff Mahoney static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_device_id *match)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	struct media_bay_info* bay;
6351da177e4SLinus Torvalds 	u32 __iomem *regbase;
6361da177e4SLinus Torvalds 	struct device_node *ofnode;
637cc5d0189SBenjamin Herrenschmidt 	unsigned long base;
6381da177e4SLinus Torvalds 	int i;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	ofnode = mdev->ofdev.node;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (macio_resource_count(mdev) < 1)
6431da177e4SLinus Torvalds 		return -ENODEV;
6441da177e4SLinus Torvalds 	if (macio_request_resources(mdev, "media-bay"))
6451da177e4SLinus Torvalds 		return -EBUSY;
6461da177e4SLinus Torvalds 	/* Media bay registers are located at the beginning of the
647cc5d0189SBenjamin Herrenschmidt          * mac-io chip, for now, we trick and align down the first
648cc5d0189SBenjamin Herrenschmidt 	 * resource passed in
6491da177e4SLinus Torvalds          */
650cc5d0189SBenjamin Herrenschmidt 	base = macio_resource_start(mdev, 0) & 0xffff0000u;
651cc5d0189SBenjamin Herrenschmidt 	regbase = (u32 __iomem *)ioremap(base, 0x100);
6521da177e4SLinus Torvalds 	if (regbase == NULL) {
6531da177e4SLinus Torvalds 		macio_release_resources(mdev);
6541da177e4SLinus Torvalds 		return -ENOMEM;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	i = media_bay_count++;
6581da177e4SLinus Torvalds 	bay = &media_bays[i];
6591da177e4SLinus Torvalds 	bay->mdev = mdev;
6601da177e4SLinus Torvalds 	bay->base = regbase;
6611da177e4SLinus Torvalds 	bay->index = i;
6621da177e4SLinus Torvalds 	bay->ops = match->data;
6631da177e4SLinus Torvalds 	bay->sleeping = 0;
664*9a24729dSDaniel Walker 	mutex_init(&bay->lock);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* Init HW probing */
6671da177e4SLinus Torvalds 	if (bay->ops->init)
6681da177e4SLinus Torvalds 		bay->ops->init(bay);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* Force an immediate detect */
6731da177e4SLinus Torvalds 	set_mb_power(bay, 0);
6741da177e4SLinus Torvalds 	msleep(MB_POWER_DELAY);
6751da177e4SLinus Torvalds 	bay->content_id = MB_NO;
6761da177e4SLinus Torvalds 	bay->last_value = bay->ops->content(bay);
6771da177e4SLinus Torvalds 	bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
6781da177e4SLinus Torvalds 	bay->state = mb_empty;
6791da177e4SLinus Torvalds 	do {
6801da177e4SLinus Torvalds 		msleep(MB_POLL_DELAY);
6811da177e4SLinus Torvalds 		media_bay_step(i);
6821da177e4SLinus Torvalds 	} while((bay->state != mb_empty) &&
6831da177e4SLinus Torvalds 		(bay->state != mb_up));
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	/* Mark us ready by filling our mdev data */
6861da177e4SLinus Torvalds 	macio_set_drvdata(mdev, bay);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	/* Startup kernel thread */
6891da177e4SLinus Torvalds 	if (i == 0)
69033f6e794SPaul Mackerras 		kthread_run(media_bay_task, NULL, "media-bay");
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	return 0;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696aacaf9bdSJon Loeliger static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct media_bay_info	*bay = macio_get_drvdata(mdev);
6991da177e4SLinus Torvalds 
7003a2d5b70SRafael J. Wysocki 	if (state.event != mdev->ofdev.dev.power.power_state.event
7013a2d5b70SRafael J. Wysocki 	    && (state.event & PM_EVENT_SLEEP)) {
702*9a24729dSDaniel Walker 		mutex_lock(&bay->lock);
7031da177e4SLinus Torvalds 		bay->sleeping = 1;
7041da177e4SLinus Torvalds 		set_mb_power(bay, 0);
705*9a24729dSDaniel Walker 		mutex_unlock(&bay->lock);
7061da177e4SLinus Torvalds 		msleep(MB_POLL_DELAY);
7071da177e4SLinus Torvalds 		mdev->ofdev.dev.power.power_state = state;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 	return 0;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
712aacaf9bdSJon Loeliger static int media_bay_resume(struct macio_dev *mdev)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	struct media_bay_info	*bay = macio_get_drvdata(mdev);
7151da177e4SLinus Torvalds 
716ca078baeSPavel Machek 	if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
717829ca9a3SPavel Machek 		mdev->ofdev.dev.power.power_state = PMSG_ON;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	       	/* We re-enable the bay using it's previous content
7201da177e4SLinus Torvalds 	       	   only if it did not change. Note those bozo timings,
7211da177e4SLinus Torvalds 	       	   they seem to help the 3400 get it right.
7221da177e4SLinus Torvalds 	       	 */
7231da177e4SLinus Torvalds 	       	/* Force MB power to 0 */
724*9a24729dSDaniel Walker 		mutex_lock(&bay->lock);
7251da177e4SLinus Torvalds 	       	set_mb_power(bay, 0);
7261da177e4SLinus Torvalds 		msleep(MB_POWER_DELAY);
7271da177e4SLinus Torvalds 	       	if (bay->ops->content(bay) != bay->content_id) {
7281da177e4SLinus Torvalds 			printk("mediabay%d: content changed during sleep...\n", bay->index);
729*9a24729dSDaniel Walker 			mutex_unlock(&bay->lock);
7301da177e4SLinus Torvalds 	       		return 0;
7311da177e4SLinus Torvalds 		}
7321da177e4SLinus Torvalds 	       	set_mb_power(bay, 1);
7331da177e4SLinus Torvalds 	       	bay->last_value = bay->content_id;
7341da177e4SLinus Torvalds 	       	bay->value_count = msecs_to_jiffies(MB_STABLE_DELAY);
7351da177e4SLinus Torvalds 	       	bay->timer = msecs_to_jiffies(MB_POWER_DELAY);
73634394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
7371da177e4SLinus Torvalds 	       	bay->cd_retry = 0;
7381da177e4SLinus Torvalds #endif
7391da177e4SLinus Torvalds 	       	do {
7401da177e4SLinus Torvalds 			msleep(MB_POLL_DELAY);
7411da177e4SLinus Torvalds 	       		media_bay_step(bay->index);
7421da177e4SLinus Torvalds 	       	} while((bay->state != mb_empty) &&
7431da177e4SLinus Torvalds 	       		(bay->state != mb_up));
7441da177e4SLinus Torvalds 		bay->sleeping = 0;
745*9a24729dSDaniel Walker 		mutex_unlock(&bay->lock);
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	return 0;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds /* Definitions of "ops" structures.
7521da177e4SLinus Torvalds  */
753aacaf9bdSJon Loeliger static struct mb_ops ohare_mb_ops = {
7541da177e4SLinus Torvalds 	.name		= "Ohare",
7551da177e4SLinus Torvalds 	.content	= ohare_mb_content,
7561da177e4SLinus Torvalds 	.power		= ohare_mb_power,
7571da177e4SLinus Torvalds 	.setup_bus	= ohare_mb_setup_bus,
7581da177e4SLinus Torvalds 	.un_reset	= ohare_mb_un_reset,
7591da177e4SLinus Torvalds 	.un_reset_ide	= ohare_mb_un_reset_ide,
7601da177e4SLinus Torvalds };
7611da177e4SLinus Torvalds 
762aacaf9bdSJon Loeliger static struct mb_ops heathrow_mb_ops = {
7631da177e4SLinus Torvalds 	.name		= "Heathrow",
7641da177e4SLinus Torvalds 	.content	= heathrow_mb_content,
7651da177e4SLinus Torvalds 	.power		= heathrow_mb_power,
7661da177e4SLinus Torvalds 	.setup_bus	= heathrow_mb_setup_bus,
7671da177e4SLinus Torvalds 	.un_reset	= heathrow_mb_un_reset,
7681da177e4SLinus Torvalds 	.un_reset_ide	= heathrow_mb_un_reset_ide,
7691da177e4SLinus Torvalds };
7701da177e4SLinus Torvalds 
771aacaf9bdSJon Loeliger static struct mb_ops keylargo_mb_ops = {
7721da177e4SLinus Torvalds 	.name		= "KeyLargo",
7731da177e4SLinus Torvalds 	.init		= keylargo_mb_init,
7741da177e4SLinus Torvalds 	.content	= keylargo_mb_content,
7751da177e4SLinus Torvalds 	.power		= keylargo_mb_power,
7761da177e4SLinus Torvalds 	.setup_bus	= keylargo_mb_setup_bus,
7771da177e4SLinus Torvalds 	.un_reset	= keylargo_mb_un_reset,
7781da177e4SLinus Torvalds 	.un_reset_ide	= keylargo_mb_un_reset_ide,
7791da177e4SLinus Torvalds };
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds /*
7821da177e4SLinus Torvalds  * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
7831da177e4SLinus Torvalds  * register is always set when there is something in the media bay.
7841da177e4SLinus Torvalds  * This causes problems for the interrupt code if we attach an interrupt
7851da177e4SLinus Torvalds  * handler to the media-bay interrupt, because it tends to go into
7861da177e4SLinus Torvalds  * an infinite loop calling the media bay interrupt handler.
7871da177e4SLinus Torvalds  * Therefore we do it all by polling the media bay once each tick.
7881da177e4SLinus Torvalds  */
7891da177e4SLinus Torvalds 
7905e655772SJeff Mahoney static struct of_device_id media_bay_match[] =
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	{
7931da177e4SLinus Torvalds 	.name		= "media-bay",
7941da177e4SLinus Torvalds 	.compatible	= "keylargo-media-bay",
7951da177e4SLinus Torvalds 	.data		= &keylargo_mb_ops,
7961da177e4SLinus Torvalds 	},
7971da177e4SLinus Torvalds 	{
7981da177e4SLinus Torvalds 	.name		= "media-bay",
7991da177e4SLinus Torvalds 	.compatible	= "heathrow-media-bay",
8001da177e4SLinus Torvalds 	.data		= &heathrow_mb_ops,
8011da177e4SLinus Torvalds 	},
8021da177e4SLinus Torvalds 	{
8031da177e4SLinus Torvalds 	.name		= "media-bay",
8041da177e4SLinus Torvalds 	.compatible	= "ohare-media-bay",
8051da177e4SLinus Torvalds 	.data		= &ohare_mb_ops,
8061da177e4SLinus Torvalds 	},
8071da177e4SLinus Torvalds 	{},
8081da177e4SLinus Torvalds };
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds static struct macio_driver media_bay_driver =
8111da177e4SLinus Torvalds {
8121da177e4SLinus Torvalds 	.name		= "media-bay",
8131da177e4SLinus Torvalds 	.match_table	= media_bay_match,
8141da177e4SLinus Torvalds 	.probe		= media_bay_attach,
8151da177e4SLinus Torvalds 	.suspend	= media_bay_suspend,
8161da177e4SLinus Torvalds 	.resume		= media_bay_resume
8171da177e4SLinus Torvalds };
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static int __init media_bay_init(void)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	int i;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	for (i=0; i<MAX_BAYS; i++) {
8241da177e4SLinus Torvalds 		memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
8251da177e4SLinus Torvalds 		media_bays[i].content_id	= -1;
82634394e45SBartlomiej Zolnierkiewicz #ifdef CONFIG_BLK_DEV_IDE_PMAC
8271da177e4SLinus Torvalds 		media_bays[i].cd_index		= -1;
8281da177e4SLinus Torvalds #endif
8291da177e4SLinus Torvalds 	}
830e8222502SBenjamin Herrenschmidt 	if (!machine_is(powermac))
831e8222502SBenjamin Herrenschmidt 		return 0;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	macio_register_driver(&media_bay_driver);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	return 0;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds device_initcall(media_bay_init);
839