Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the media bay on the PowerBook 3400 and 2400. |
| 3 | * |
| 4 | * Copyright (C) 1998 Paul Mackerras. |
| 5 | * |
| 6 | * Various evolutions by Benjamin Herrenschmidt & Henry Worth |
| 7 | * |
| 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. |
| 12 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/stddef.h> |
| 20 | #include <linux/init.h> |
Paul Mackerras | 33f6e79 | 2007-12-13 14:12:58 +1100 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/prom.h> |
| 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/io.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> |
| 35 | |
| 36 | |
| 37 | #define MB_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #ifdef MB_DEBUG |
| 40 | #define MBDBG(fmt, arg...) printk(KERN_INFO fmt , ## arg) |
| 41 | #else |
| 42 | #define MBDBG(fmt, arg...) do { } while (0) |
| 43 | #endif |
| 44 | |
| 45 | #define MB_FCR32(bay, r) ((bay)->base + ((r) >> 2)) |
| 46 | #define MB_FCR8(bay, r) (((volatile u8 __iomem *)((bay)->base)) + (r)) |
| 47 | |
| 48 | #define MB_IN32(bay,r) (in_le32(MB_FCR32(bay,r))) |
| 49 | #define MB_OUT32(bay,r,v) (out_le32(MB_FCR32(bay,r), (v))) |
| 50 | #define MB_BIS(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v))) |
| 51 | #define MB_BIC(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v))) |
| 52 | #define MB_IN8(bay,r) (in_8(MB_FCR8(bay,r))) |
| 53 | #define MB_OUT8(bay,r,v) (out_8(MB_FCR8(bay,r), (v))) |
| 54 | |
| 55 | struct media_bay_info; |
| 56 | |
| 57 | struct mb_ops { |
| 58 | char* name; |
| 59 | void (*init)(struct media_bay_info *bay); |
| 60 | u8 (*content)(struct media_bay_info *bay); |
| 61 | void (*power)(struct media_bay_info *bay, int on_off); |
| 62 | int (*setup_bus)(struct media_bay_info *bay, u8 device_id); |
| 63 | void (*un_reset)(struct media_bay_info *bay); |
| 64 | void (*un_reset_ide)(struct media_bay_info *bay); |
| 65 | }; |
| 66 | |
| 67 | struct media_bay_info { |
| 68 | u32 __iomem *base; |
| 69 | int content_id; |
| 70 | int state; |
| 71 | int last_value; |
| 72 | int value_count; |
| 73 | int timer; |
| 74 | struct macio_dev *mdev; |
| 75 | struct mb_ops* ops; |
| 76 | int index; |
| 77 | int cached_gpio; |
| 78 | int sleeping; |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 79 | struct mutex lock; |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 80 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Bartlomiej Zolnierkiewicz | 2dde786 | 2008-04-18 00:46:23 +0200 | [diff] [blame] | 81 | ide_hwif_t *cd_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | void __iomem *cd_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int cd_irq; |
| 84 | int cd_retry; |
| 85 | #endif |
Adrian Bunk | 305c736 | 2008-06-11 03:47:45 +1000 | [diff] [blame] | 86 | #if defined(CONFIG_BLK_DEV_IDE_PMAC) |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 87 | int cd_index; |
| 88 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #define MAX_BAYS 2 |
| 92 | |
| 93 | static struct media_bay_info media_bays[MAX_BAYS]; |
| 94 | int media_bay_count = 0; |
| 95 | |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 96 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* check the busy bit in the media-bay ide interface |
| 98 | (assumes the media-bay contains an ide device) */ |
| 99 | #define MB_IDE_READY(i) ((readb(media_bays[i].cd_base + 0x70) & 0x80) == 0) |
| 100 | #endif |
| 101 | |
| 102 | /* |
| 103 | * Wait that number of ms between each step in normal polling mode |
| 104 | */ |
| 105 | #define MB_POLL_DELAY 25 |
| 106 | |
| 107 | /* |
| 108 | * Consider the media-bay ID value stable if it is the same for |
| 109 | * this number of milliseconds |
| 110 | */ |
| 111 | #define MB_STABLE_DELAY 100 |
| 112 | |
| 113 | /* Wait after powering up the media bay this delay in ms |
| 114 | * timeout bumped for some powerbooks |
| 115 | */ |
| 116 | #define MB_POWER_DELAY 200 |
| 117 | |
| 118 | /* |
| 119 | * Hold the media-bay reset signal true for this many ticks |
| 120 | * after a device is inserted before releasing it. |
| 121 | */ |
| 122 | #define MB_RESET_DELAY 50 |
| 123 | |
| 124 | /* |
| 125 | * Wait this long after the reset signal is released and before doing |
| 126 | * further operations. After this delay, the IDE reset signal is released |
| 127 | * too for an IDE device |
| 128 | */ |
| 129 | #define MB_SETUP_DELAY 100 |
| 130 | |
| 131 | /* |
| 132 | * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted |
| 133 | * (or until the device is ready) before waiting for busy bit to disappear |
| 134 | */ |
| 135 | #define MB_IDE_WAIT 1000 |
| 136 | |
| 137 | /* |
| 138 | * Timeout waiting for busy bit of an IDE device to go down |
| 139 | */ |
| 140 | #define MB_IDE_TIMEOUT 5000 |
| 141 | |
| 142 | /* |
| 143 | * Max retries of the full power up/down sequence for an IDE device |
| 144 | */ |
| 145 | #define MAX_CD_RETRIES 3 |
| 146 | |
| 147 | /* |
| 148 | * States of a media bay |
| 149 | */ |
| 150 | enum { |
| 151 | mb_empty = 0, /* Idle */ |
| 152 | mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */ |
| 153 | mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */ |
| 154 | mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */ |
| 155 | mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */ |
| 156 | mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */ |
| 157 | mb_up, /* Media bay full */ |
| 158 | mb_powering_down /* Powering down (avoid too fast down/up) */ |
| 159 | }; |
| 160 | |
| 161 | #define MB_POWER_SOUND 0x08 |
| 162 | #define MB_POWER_FLOPPY 0x04 |
| 163 | #define MB_POWER_ATA 0x02 |
| 164 | #define MB_POWER_PCI 0x01 |
| 165 | #define MB_POWER_OFF 0x00 |
| 166 | |
| 167 | /* |
| 168 | * Functions for polling content of media bay |
| 169 | */ |
| 170 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 171 | static u8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | ohare_mb_content(struct media_bay_info *bay) |
| 173 | { |
| 174 | return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7; |
| 175 | } |
| 176 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 177 | static u8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | heathrow_mb_content(struct media_bay_info *bay) |
| 179 | { |
| 180 | return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7; |
| 181 | } |
| 182 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 183 | static u8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | keylargo_mb_content(struct media_bay_info *bay) |
| 185 | { |
| 186 | int new_gpio; |
| 187 | |
| 188 | new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA; |
| 189 | if (new_gpio) { |
| 190 | bay->cached_gpio = new_gpio; |
| 191 | return MB_NO; |
| 192 | } else if (bay->cached_gpio != new_gpio) { |
| 193 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE); |
| 194 | (void)MB_IN32(bay, KEYLARGO_MBCR); |
| 195 | udelay(5); |
| 196 | MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F); |
| 197 | (void)MB_IN32(bay, KEYLARGO_MBCR); |
| 198 | udelay(5); |
| 199 | bay->cached_gpio = new_gpio; |
| 200 | } |
| 201 | return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Functions for powering up/down the bay, puts the bay device |
| 206 | * into reset state as well |
| 207 | */ |
| 208 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 209 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | ohare_mb_power(struct media_bay_info* bay, int on_off) |
| 211 | { |
| 212 | if (on_off) { |
| 213 | /* Power up device, assert it's reset line */ |
| 214 | MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N); |
| 215 | MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N); |
| 216 | } else { |
| 217 | /* Disable all devices */ |
| 218 | MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK); |
| 219 | MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE); |
| 220 | /* Cut power from bay, release reset line */ |
| 221 | MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N); |
| 222 | MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N); |
| 223 | MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N); |
| 224 | } |
| 225 | MB_BIC(bay, OHARE_MBCR, 0x00000F00); |
| 226 | } |
| 227 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 228 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | heathrow_mb_power(struct media_bay_info* bay, int on_off) |
| 230 | { |
| 231 | if (on_off) { |
| 232 | /* Power up device, assert it's reset line */ |
| 233 | MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N); |
| 234 | MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N); |
| 235 | } else { |
| 236 | /* Disable all devices */ |
| 237 | MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK); |
| 238 | MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE); |
| 239 | /* Cut power from bay, release reset line */ |
| 240 | MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N); |
| 241 | MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N); |
| 242 | MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N); |
| 243 | } |
| 244 | MB_BIC(bay, HEATHROW_MBCR, 0x00000F00); |
| 245 | } |
| 246 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 247 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | keylargo_mb_power(struct media_bay_info* bay, int on_off) |
| 249 | { |
| 250 | if (on_off) { |
| 251 | /* Power up device, assert it's reset line */ |
| 252 | MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET); |
| 253 | MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER); |
| 254 | } else { |
| 255 | /* Disable all devices */ |
| 256 | MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK); |
| 257 | MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE); |
| 258 | /* Cut power from bay, release reset line */ |
| 259 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER); |
| 260 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET); |
| 261 | MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N); |
| 262 | } |
| 263 | MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F); |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Functions for configuring the media bay for a given type of device, |
| 268 | * enable the related busses |
| 269 | */ |
| 270 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 271 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id) |
| 273 | { |
| 274 | switch(device_id) { |
| 275 | case MB_FD: |
| 276 | case MB_FD1: |
| 277 | MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE); |
| 278 | MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE); |
| 279 | return 0; |
| 280 | case MB_CD: |
| 281 | MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N); |
| 282 | MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE); |
| 283 | return 0; |
| 284 | case MB_PCI: |
| 285 | MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE); |
| 286 | return 0; |
| 287 | } |
| 288 | return -ENODEV; |
| 289 | } |
| 290 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 291 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id) |
| 293 | { |
| 294 | switch(device_id) { |
| 295 | case MB_FD: |
| 296 | case MB_FD1: |
| 297 | MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE); |
| 298 | MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE); |
| 299 | return 0; |
| 300 | case MB_CD: |
| 301 | MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N); |
| 302 | MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE); |
| 303 | return 0; |
| 304 | case MB_PCI: |
| 305 | MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE); |
| 306 | return 0; |
| 307 | } |
| 308 | return -ENODEV; |
| 309 | } |
| 310 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 311 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id) |
| 313 | { |
| 314 | switch(device_id) { |
| 315 | case MB_CD: |
| 316 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE); |
| 317 | MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N); |
| 318 | MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE); |
| 319 | return 0; |
| 320 | case MB_PCI: |
| 321 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE); |
| 322 | return 0; |
| 323 | case MB_SOUND: |
| 324 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE); |
| 325 | return 0; |
| 326 | } |
| 327 | return -ENODEV; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Functions for tweaking resets |
| 332 | */ |
| 333 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 334 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | ohare_mb_un_reset(struct media_bay_info* bay) |
| 336 | { |
| 337 | MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N); |
| 338 | } |
| 339 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 340 | static void keylargo_mb_init(struct media_bay_info *bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE); |
| 343 | } |
| 344 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 345 | static void heathrow_mb_un_reset(struct media_bay_info* bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N); |
| 348 | } |
| 349 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 350 | static void keylargo_mb_un_reset(struct media_bay_info* bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
| 352 | MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET); |
| 353 | } |
| 354 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 355 | static void ohare_mb_un_reset_ide(struct media_bay_info* bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N); |
| 358 | } |
| 359 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 360 | static void heathrow_mb_un_reset_ide(struct media_bay_info* bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N); |
| 363 | } |
| 364 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 365 | static void keylargo_mb_un_reset_ide(struct media_bay_info* bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
| 367 | MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N); |
| 368 | } |
| 369 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 370 | static inline void set_mb_power(struct media_bay_info* bay, int onoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
| 372 | /* Power up up and assert the bay reset line */ |
| 373 | if (onoff) { |
| 374 | bay->ops->power(bay, 1); |
| 375 | bay->state = mb_powering_up; |
| 376 | MBDBG("mediabay%d: powering up\n", bay->index); |
| 377 | } else { |
| 378 | /* Make sure everything is powered down & disabled */ |
| 379 | bay->ops->power(bay, 0); |
| 380 | bay->state = mb_powering_down; |
| 381 | MBDBG("mediabay%d: powering down\n", bay->index); |
| 382 | } |
| 383 | bay->timer = msecs_to_jiffies(MB_POWER_DELAY); |
| 384 | } |
| 385 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 386 | static void poll_media_bay(struct media_bay_info* bay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | int id = bay->ops->content(bay); |
| 389 | |
| 390 | if (id == bay->last_value) { |
| 391 | if (id != bay->content_id) { |
| 392 | bay->value_count += msecs_to_jiffies(MB_POLL_DELAY); |
| 393 | if (bay->value_count >= msecs_to_jiffies(MB_STABLE_DELAY)) { |
| 394 | /* If the device type changes without going thru |
| 395 | * "MB_NO", we force a pass by "MB_NO" to make sure |
| 396 | * things are properly reset |
| 397 | */ |
| 398 | if ((id != MB_NO) && (bay->content_id != MB_NO)) { |
| 399 | id = MB_NO; |
| 400 | MBDBG("mediabay%d: forcing MB_NO\n", bay->index); |
| 401 | } |
| 402 | MBDBG("mediabay%d: switching to %d\n", bay->index, id); |
| 403 | set_mb_power(bay, id != MB_NO); |
| 404 | bay->content_id = id; |
| 405 | if (id == MB_NO) { |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 406 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | bay->cd_retry = 0; |
| 408 | #endif |
| 409 | printk(KERN_INFO "media bay %d is empty\n", bay->index); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } else { |
| 414 | bay->last_value = id; |
| 415 | bay->value_count = 0; |
| 416 | } |
| 417 | } |
| 418 | |
Adrian Bunk | 305c736 | 2008-06-11 03:47:45 +1000 | [diff] [blame] | 419 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 420 | int check_media_bay(struct device_node *which_bay, int what) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | int i; |
| 423 | |
| 424 | for (i=0; i<media_bay_count; i++) |
| 425 | if (media_bays[i].mdev && which_bay == media_bays[i].mdev->ofdev.node) { |
| 426 | if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up) |
| 427 | return 0; |
| 428 | media_bays[i].cd_index = -1; |
| 429 | return -EINVAL; |
| 430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | return -ENODEV; |
| 432 | } |
| 433 | EXPORT_SYMBOL(check_media_bay); |
| 434 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 435 | int check_media_bay_by_base(unsigned long base, int what) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | int i; |
| 438 | |
| 439 | for (i=0; i<media_bay_count; i++) |
| 440 | if (media_bays[i].mdev && base == (unsigned long) media_bays[i].cd_base) { |
| 441 | if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up) |
| 442 | return 0; |
| 443 | media_bays[i].cd_index = -1; |
| 444 | return -EINVAL; |
| 445 | } |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return -ENODEV; |
| 448 | } |
Bartlomiej Zolnierkiewicz | 6329db8 | 2009-04-21 09:04:37 +0000 | [diff] [blame] | 449 | EXPORT_SYMBOL_GPL(check_media_bay_by_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 451 | int media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base, |
Bartlomiej Zolnierkiewicz | 2dde786 | 2008-04-18 00:46:23 +0200 | [diff] [blame] | 452 | int irq, ide_hwif_t *hwif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | int i; |
| 455 | |
| 456 | for (i=0; i<media_bay_count; i++) { |
| 457 | struct media_bay_info* bay = &media_bays[i]; |
| 458 | |
| 459 | if (bay->mdev && which_bay == bay->mdev->ofdev.node) { |
Bartlomiej Zolnierkiewicz | 2dde786 | 2008-04-18 00:46:23 +0200 | [diff] [blame] | 460 | int timeout = 5000, index = hwif->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 462 | mutex_lock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Bartlomiej Zolnierkiewicz | 2dde786 | 2008-04-18 00:46:23 +0200 | [diff] [blame] | 464 | bay->cd_port = hwif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | bay->cd_base = (void __iomem *) base; |
| 466 | bay->cd_irq = irq; |
| 467 | |
| 468 | if ((MB_CD != bay->content_id) || bay->state != mb_up) { |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 469 | mutex_unlock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | printk(KERN_DEBUG "Registered ide%d for media bay %d\n", index, i); |
| 473 | do { |
| 474 | if (MB_IDE_READY(i)) { |
| 475 | bay->cd_index = index; |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 476 | mutex_unlock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | mdelay(1); |
| 480 | } while(--timeout); |
| 481 | printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i); |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 482 | mutex_unlock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return -ENODEV; |
| 484 | } |
| 485 | } |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return -ENODEV; |
| 488 | } |
Bartlomiej Zolnierkiewicz | 6329db8 | 2009-04-21 09:04:37 +0000 | [diff] [blame] | 489 | EXPORT_SYMBOL_GPL(media_bay_set_ide_infos); |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 490 | #endif /* CONFIG_BLK_DEV_IDE_PMAC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 492 | static void media_bay_step(int i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | struct media_bay_info* bay = &media_bays[i]; |
| 495 | |
| 496 | /* We don't poll when powering down */ |
| 497 | if (bay->state != mb_powering_down) |
| 498 | poll_media_bay(bay); |
| 499 | |
| 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; |
| 506 | } |
| 507 | |
| 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; |
| 514 | } |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 530 | } |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 531 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 539 | #endif /* CONFIG_BLK_DEV_IDE_PMAC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | break; |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 541 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | printk("mediabay %d, registering IDE...\n", i); |
| 558 | pmu_suspend(); |
Bartlomiej Zolnierkiewicz | 2dde786 | 2008-04-18 00:46:23 +0200 | [diff] [blame] | 559 | ide_port_scan(bay->cd_port); |
Bartlomiej Zolnierkiewicz | c1a8e39 | 2008-06-15 21:00:23 +0200 | [diff] [blame] | 560 | if (bay->cd_port->present) |
| 561 | bay->cd_index = bay->cd_port->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | pmu_resume(); |
| 563 | } |
| 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); |
| 572 | } |
| 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; |
| 582 | } |
| 583 | break; |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 584 | #endif /* CONFIG_BLK_DEV_IDE_PMAC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | case mb_powering_down: |
| 586 | bay->state = mb_empty; |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 587 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (bay->cd_index >= 0) { |
| 589 | printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i, |
| 590 | bay->cd_index); |
Bartlomiej Zolnierkiewicz | 2dde786 | 2008-04-18 00:46:23 +0200 | [diff] [blame] | 591 | ide_port_unregister_devices(bay->cd_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | bay->cd_index = -1; |
| 593 | } |
| 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; |
| 601 | } |
| 602 | } |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 603 | #endif /* CONFIG_BLK_DEV_IDE_PMAC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | MBDBG("mediabay%d: end of power down\n", i); |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | /* |
| 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. |
| 614 | */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 615 | static int media_bay_task(void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
| 617 | int i; |
| 618 | |
Paul Mackerras | 33f6e79 | 2007-12-13 14:12:58 +1100 | [diff] [blame] | 619 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | for (i = 0; i < media_bay_count; ++i) { |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 621 | mutex_lock(&media_bays[i].lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (!media_bays[i].sleeping) |
| 623 | media_bay_step(i); |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 624 | mutex_unlock(&media_bays[i].lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | msleep_interruptible(MB_POLL_DELAY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
Paul Mackerras | 33f6e79 | 2007-12-13 14:12:58 +1100 | [diff] [blame] | 629 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 632 | static int __devinit media_bay_attach(struct macio_dev *mdev, const struct of_device_id *match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
| 634 | struct media_bay_info* bay; |
| 635 | u32 __iomem *regbase; |
| 636 | struct device_node *ofnode; |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 637 | unsigned long base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | int i; |
| 639 | |
| 640 | ofnode = mdev->ofdev.node; |
| 641 | |
| 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 |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 647 | * mac-io chip, for now, we trick and align down the first |
| 648 | * resource passed in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | */ |
Benjamin Herrenschmidt | cc5d018 | 2005-12-13 18:01:21 +1100 | [diff] [blame] | 650 | base = macio_resource_start(mdev, 0) & 0xffff0000u; |
| 651 | regbase = (u32 __iomem *)ioremap(base, 0x100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (regbase == NULL) { |
| 653 | macio_release_resources(mdev); |
| 654 | return -ENOMEM; |
| 655 | } |
| 656 | |
| 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; |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 664 | mutex_init(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | /* Init HW probing */ |
| 667 | if (bay->ops->init) |
| 668 | bay->ops->init(bay); |
| 669 | |
| 670 | printk(KERN_INFO "mediabay%d: Registered %s media-bay\n", i, bay->ops->name); |
| 671 | |
| 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)); |
| 684 | |
| 685 | /* Mark us ready by filling our mdev data */ |
| 686 | macio_set_drvdata(mdev, bay); |
| 687 | |
| 688 | /* Startup kernel thread */ |
| 689 | if (i == 0) |
Paul Mackerras | 33f6e79 | 2007-12-13 14:12:58 +1100 | [diff] [blame] | 690 | kthread_run(media_bay_task, NULL, "media-bay"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | return 0; |
| 693 | |
| 694 | } |
| 695 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 696 | static int media_bay_suspend(struct macio_dev *mdev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
| 698 | struct media_bay_info *bay = macio_get_drvdata(mdev); |
| 699 | |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 700 | if (state.event != mdev->ofdev.dev.power.power_state.event |
| 701 | && (state.event & PM_EVENT_SLEEP)) { |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 702 | mutex_lock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | bay->sleeping = 1; |
| 704 | set_mb_power(bay, 0); |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 705 | mutex_unlock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | msleep(MB_POLL_DELAY); |
| 707 | mdev->ofdev.dev.power.power_state = state; |
| 708 | } |
| 709 | return 0; |
| 710 | } |
| 711 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 712 | static int media_bay_resume(struct macio_dev *mdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
| 714 | struct media_bay_info *bay = macio_get_drvdata(mdev); |
| 715 | |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 716 | if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) { |
Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 717 | mdev->ofdev.dev.power.power_state = PMSG_ON; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 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. |
| 722 | */ |
| 723 | /* Force MB power to 0 */ |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 724 | mutex_lock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 729 | mutex_unlock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | return 0; |
| 731 | } |
| 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); |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 736 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Daniel Walker | 9a24729 | 2008-06-10 09:26:09 +1000 | [diff] [blame] | 745 | mutex_unlock(&bay->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | /* Definitions of "ops" structures. |
| 752 | */ |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 753 | static struct mb_ops ohare_mb_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
| 760 | }; |
| 761 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 762 | static struct mb_ops heathrow_mb_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
| 769 | }; |
| 770 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 771 | static struct mb_ops keylargo_mb_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
| 779 | }; |
| 780 | |
| 781 | /* |
| 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. |
| 788 | */ |
| 789 | |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 790 | static struct of_device_id media_bay_match[] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
| 792 | { |
| 793 | .name = "media-bay", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | .compatible = "keylargo-media-bay", |
| 795 | .data = &keylargo_mb_ops, |
| 796 | }, |
| 797 | { |
| 798 | .name = "media-bay", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | .compatible = "heathrow-media-bay", |
| 800 | .data = &heathrow_mb_ops, |
| 801 | }, |
| 802 | { |
| 803 | .name = "media-bay", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | .compatible = "ohare-media-bay", |
| 805 | .data = &ohare_mb_ops, |
| 806 | }, |
| 807 | {}, |
| 808 | }; |
| 809 | |
| 810 | static struct macio_driver media_bay_driver = |
| 811 | { |
| 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 |
| 817 | }; |
| 818 | |
| 819 | static int __init media_bay_init(void) |
| 820 | { |
| 821 | int i; |
| 822 | |
| 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; |
Bartlomiej Zolnierkiewicz | 34394e4 | 2008-02-06 02:57:50 +0100 | [diff] [blame] | 826 | #ifdef CONFIG_BLK_DEV_IDE_PMAC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | media_bays[i].cd_index = -1; |
| 828 | #endif |
| 829 | } |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 830 | if (!machine_is(powermac)) |
| 831 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | macio_register_driver(&media_bay_driver); |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | device_initcall(media_bay_init); |