~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/macintosh/mediabay.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  */
 13 #define __KERNEL_SYSCALLS__
 14 
 15 #include <linux/config.h>
 16 #include <linux/types.h>
 17 #include <linux/errno.h>
 18 #include <linux/kernel.h>
 19 #include <linux/delay.h>
 20 #include <linux/sched.h>
 21 #include <linux/timer.h>
 22 #include <linux/hdreg.h>
 23 #include <linux/stddef.h>
 24 #include <linux/unistd.h>
 25 #include <asm/prom.h>
 26 #include <asm/pgtable.h>
 27 #include <asm/io.h>
 28 #include <asm/feature.h>
 29 #include <asm/mediabay.h>
 30 #include <asm/init.h>
 31 #include <linux/adb.h>
 32 #include <linux/pmu.h>
 33 
 34 #ifdef CONFIG_PMAC_PBOOK
 35 static int mb_notify_sleep(struct pmu_sleep_notifier *self, int when);
 36 static struct pmu_sleep_notifier mb_sleep_notifier = {
 37         mb_notify_sleep,
 38         SLEEP_LEVEL_MEDIABAY,
 39 };
 40 #endif
 41 
 42 #undef MB_USE_INTERRUPTS
 43 #undef MB_DEBUG
 44 #define MB_IGNORE_SIGNALS
 45 
 46 #ifdef MB_DEBUG
 47 #define MBDBG(fmt, arg...)      printk(KERN_INFO fmt , ## arg)
 48 #else
 49 #define MBDBG(fmt, arg...)      do { } while (0)
 50 #endif
 51 
 52 struct media_bay_hw {
 53         unsigned char   b0;
 54         unsigned char   contents;
 55         unsigned char   b2;
 56         unsigned char   b3;
 57 };
 58 
 59 struct media_bay_info {
 60         volatile struct media_bay_hw*   addr;
 61         volatile u8*                    extint_gpio;
 62         int                             content_id;
 63         int                             state;
 64         int                             last_value;
 65         int                             value_count;
 66         int                             timer;
 67         struct device_node*             dev_node;
 68         int                             pismo;  /* New PowerBook3,1 */
 69         int                             gpio_cache;
 70 #ifdef CONFIG_BLK_DEV_IDE
 71         unsigned long                   cd_base;
 72         int                             cd_index;
 73         int                             cd_irq;
 74         int                             cd_retry;
 75 #endif
 76 };
 77 
 78 #define MAX_BAYS        2
 79 
 80 static volatile struct media_bay_info media_bays[MAX_BAYS];
 81 int media_bay_count = 0;
 82 
 83 inline int mb_content(volatile struct media_bay_info *bay)
 84 {
 85         if (bay->pismo) {
 86                 unsigned char new_gpio = in_8(bay->extint_gpio + 0xe) & 2;
 87                 if (new_gpio) {
 88                         bay->gpio_cache = new_gpio;
 89                         return MB_NO;
 90                 } else if (bay->gpio_cache != new_gpio) {
 91                         /* make sure content bits are set */
 92                         feature_set(bay->dev_node, FEATURE_Mediabay_content);
 93                         udelay(5);
 94                         bay->gpio_cache = new_gpio;
 95                 }
 96                 return (in_le32((unsigned*)bay->addr) >> 4) & 0xf;
 97         } else {
 98                 int cont = (in_8(&bay->addr->contents) >> 4) & 7;
 99                 return (cont == 7) ? MB_NO : cont;
100         }
101 }
102 
103 #ifdef CONFIG_BLK_DEV_IDE
104 /* check the busy bit in the media-bay ide interface
105    (assumes the media-bay contains an ide device) */
106 //#define MB_IDE_READY(i)       ((inb(media_bays[i].cd_base + 0x70) & 0xc0) == 0x40)
107 #define MB_IDE_READY(i) ((inb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
108 #endif
109 
110 /* Note: All delays are not in milliseconds and converted to HZ relative
111  * values by the macro below
112  */
113 #define MS_TO_HZ(ms)    ((ms * HZ) / 1000)
114 
115 /*
116  * Consider the media-bay ID value stable if it is the same for
117  * this number of milliseconds
118  */
119 #define MB_STABLE_DELAY 40
120 
121 /* Wait after powering up the media bay this delay in ms
122  * timeout bumped for some powerbooks
123  */
124 #define MB_POWER_DELAY  200
125 
126 /*
127  * Hold the media-bay reset signal true for this many ticks
128  * after a device is inserted before releasing it.
129  */
130 #define MB_RESET_DELAY  40
131 
132 /*
133  * Wait this long after the reset signal is released and before doing
134  * further operations. After this delay, the IDE reset signal is released
135  * too for an IDE device
136  */
137 #define MB_SETUP_DELAY  100
138 
139 /*
140  * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
141  * (or until the device is ready) before waiting for busy bit to disappear
142  */
143 #define MB_IDE_WAIT     1000
144 
145 /*
146  * Timeout waiting for busy bit of an IDE device to go down
147  */
148 #define MB_IDE_TIMEOUT  5000
149 
150 /*
151  * Max retries of the full power up/down sequence for an IDE device
152  */
153 #define MAX_CD_RETRIES  3
154 
155 /*
156  * States of a media bay
157  */
158 enum {
159         mb_empty = 0,           /* Idle */
160         mb_powering_up,         /* power bit set, waiting MB_POWER_DELAY */
161         mb_enabling_bay,        /* enable bits set, waiting MB_RESET_DELAY */
162         mb_resetting,           /* reset bit unset, waiting MB_SETUP_DELAY */
163         mb_ide_resetting,       /* IDE reset bit unser, waiting MB_IDE_WAIT */
164         mb_ide_waiting,         /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
165         mb_up,                  /* Media bay full */
166         mb_powering_down        /* Powering down (avoid too fast down/up) */
167 };
168 
169 static void poll_media_bay(int which);
170 static void set_media_bay(int which, int id);
171 static void set_mb_power(int which, int onoff);
172 static void media_bay_step(int i);
173 static int media_bay_task(void *);
174 
175 #ifdef MB_USE_INTERRUPTS
176 static void media_bay_intr(int irq, void *devid, struct pt_regs *regs);
177 #endif
178 
179 /*
180  * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
181  * register is always set when there is something in the media bay.
182  * This causes problems for the interrupt code if we attach an interrupt
183  * handler to the media-bay interrupt, because it tends to go into
184  * an infinite loop calling the media bay interrupt handler.
185  * Therefore we do it all by polling the media bay once each tick.
186  */
187 
188 void __pmac
189 media_bay_init(void)
190 {
191         struct device_node *np;
192         int             n,i;
193         
194         for (i=0; i<MAX_BAYS; i++) {
195                 memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
196                 media_bays[i].content_id        = -1;
197 #ifdef CONFIG_BLK_DEV_IDE
198                 media_bays[i].cd_index          = -1;
199 #endif
200         }
201         
202         np = find_devices("media-bay");
203         n = 0;
204         while(np && (n<MAX_BAYS)) {
205                 if (np->n_addrs == 0)
206                         continue;
207                 media_bays[n].addr = (volatile struct media_bay_hw *)
208                         ioremap(np->addrs[0].address, sizeof(struct media_bay_hw));
209 
210                 media_bays[n].pismo = device_is_compatible(np, "keylargo-media-bay");
211                 if (media_bays[n].pismo) {
212                         if (!np->parent || strcmp(np->parent->name, "mac-io")) {
213                                 printk(KERN_ERR "Pismo media-bay has no mac-io parent !\n");
214                                 continue;
215                         }
216                         media_bays[n].extint_gpio = ioremap(np->parent->addrs[0].address
217                                 + 0x58, 0x10);
218                 }
219 
220 #ifdef MB_USE_INTERRUPTS
221                 if (np->n_intrs == 0) {
222                         printk(KERN_ERR "media bay %d has no irq\n",n);
223                         continue;
224                 }
225 
226                 if (request_irq(np->intrs[0].line, media_bay_intr, 0, "Media bay", (void *)n)) {
227                         printk(KERN_ERR "Couldn't get IRQ %d for media bay %d\n",
228                                 np->intrs[0].line, n);
229                         continue;
230                 }
231 #endif  
232                 media_bay_count++;
233         
234                 media_bays[n].dev_node          = np;
235 
236                 /* Force an immediate detect */
237                 set_mb_power(n,0);
238                 mdelay(MB_POWER_DELAY);
239                 if(!media_bays[n].pismo)
240                         out_8(&media_bays[n].addr->contents, 0x70);
241                 mdelay(MB_STABLE_DELAY);
242                 media_bays[n].content_id = MB_NO;
243                 media_bays[n].last_value = mb_content(&media_bays[n]);
244                 media_bays[n].value_count = MS_TO_HZ(MB_STABLE_DELAY);
245                 media_bays[n].state = mb_empty;
246                 do {
247                         mdelay(1000/HZ);
248                         media_bay_step(n);
249                 } while((media_bays[n].state != mb_empty) &&
250                         (media_bays[n].state != mb_up));
251 
252                 n++;
253                 np=np->next;
254         }
255 
256         if (media_bay_count)
257         {
258                 printk(KERN_INFO "Registered %d media-bay(s)\n", media_bay_count);
259 
260 #ifdef CONFIG_PMAC_PBOOK
261                 pmu_register_sleep_notifier(&mb_sleep_notifier);
262 #endif /* CONFIG_PMAC_PBOOK */
263 
264                 kernel_thread(media_bay_task, NULL,
265                               CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
266         }
267 }
268 
269 #ifdef MB_USE_INTERRUPTS
270 static void __pmac
271 media_bay_intr(int irq, void *devid, struct pt_regs *regs)
272 {
273 }
274 #endif
275 
276 static void __pmac
277 set_mb_power(int which, int onoff)
278 {
279         volatile struct media_bay_info* mb = &media_bays[which];
280 
281         if (onoff) {
282                 feature_set(mb->dev_node, FEATURE_Mediabay_power);
283                 udelay(10);
284                 feature_set(mb->dev_node, FEATURE_Mediabay_reset);
285                 udelay(10);
286                 mb->state = mb_powering_up;
287                 MBDBG("mediabay%d: powering up\n", which);
288         } else {
289                 feature_clear(mb->dev_node, FEATURE_Mediabay_floppy_enable);
290                 if (mb->pismo)
291                         feature_clear(mb->dev_node, FEATURE_IDE0_enable);
292                 else
293                         feature_clear(mb->dev_node, FEATURE_IDE1_enable);
294                 feature_clear(mb->dev_node, FEATURE_Mediabay_IDE_switch);
295                 feature_clear(mb->dev_node, FEATURE_Mediabay_PCI_enable);
296                 feature_clear(mb->dev_node, FEATURE_SWIM3_enable);
297                 feature_clear(mb->dev_node, FEATURE_Mediabay_power);
298                 mb->state = mb_powering_down;
299                 MBDBG("mediabay%d: powering down\n", which);
300         }
301         mb->timer = MS_TO_HZ(MB_POWER_DELAY);
302 }
303 
304 static void __pmac
305 set_media_bay(int which, int id)
306 {
307         volatile struct media_bay_info* bay;
308 
309         bay = &media_bays[which];
310         
311         switch (id) {
312         case MB_CD:
313                 if (bay->pismo) {
314                         feature_set(bay->dev_node, FEATURE_Mediabay_IDE_switch);
315                         udelay(10);
316                         feature_set(bay->dev_node, FEATURE_IDE0_enable);
317                         udelay(10);
318                         feature_set(bay->dev_node, FEATURE_IDE0_reset);
319                 } else {
320                         feature_set(bay->dev_node, FEATURE_IDE1_enable);
321                         udelay(10);
322                         feature_set(bay->dev_node, FEATURE_IDE1_reset);
323                 }
324                 printk(KERN_INFO "media bay %d contains a CD-ROM drive\n", which);
325                 break;
326         case MB_FD:
327         case MB_FD1:
328                 feature_set(bay->dev_node, FEATURE_Mediabay_floppy_enable);
329                 feature_set(bay->dev_node, FEATURE_SWIM3_enable);
330                 printk(KERN_INFO "media bay %d contains a floppy disk drive\n", which);
331                 break;
332         case MB_NO:
333                 break;
334         default:
335                 printk(KERN_INFO "media bay %d contains an unknown device (%d)\n",
336                        which, id);
337                 break;
338         }
339 }
340 
341 int __pmac
342 check_media_bay(struct device_node *which_bay, int what)
343 {
344 #ifdef CONFIG_BLK_DEV_IDE
345         int     i;
346 
347         for (i=0; i<media_bay_count; i++)
348                 if (which_bay == media_bays[i].dev_node)
349                 {
350                         if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
351                                 return 0;
352                         media_bays[i].cd_index = -1;
353                         return -EINVAL;
354                 }
355 #endif /* CONFIG_BLK_DEV_IDE */
356         return -ENODEV;
357 }
358 
359 int __pmac
360 check_media_bay_by_base(unsigned long base, int what)
361 {
362 #ifdef CONFIG_BLK_DEV_IDE
363         int     i;
364 
365         for (i=0; i<media_bay_count; i++)
366                 if (base == media_bays[i].cd_base)
367                 {
368                         if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
369                                 return 0;
370                         media_bays[i].cd_index = -1;
371                         return -EINVAL;
372                 } 
373 #endif
374         
375         return -ENODEV;
376 }
377 
378 int __pmac
379 media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
380         int irq, int index)
381 {
382 #ifdef CONFIG_BLK_DEV_IDE
383         int     i;
384 
385         for (i=0; i<media_bay_count; i++)
386                 if (which_bay == media_bays[i].dev_node)
387                 {
388                         int timeout = 5000;
389                         
390                         media_bays[i].cd_base   = base;
391                         media_bays[i].cd_irq    = irq;
392 
393                         if ((MB_CD != media_bays[i].content_id) || media_bays[i].state != mb_up)
394                                 return 0;
395 
396                         printk(KERN_DEBUG "Registered ide %d for media bay %d\n", index, i);
397                         do {
398                                 if (MB_IDE_READY(i)) {
399                                         media_bays[i].cd_index  = index;
400                                         return 0;
401                                 }
402                                 mdelay(1);
403                         } while(--timeout);
404                         printk(KERN_DEBUG "Timeount waiting IDE in bay %d\n", i);
405                         return -ENODEV;
406                 } 
407 #endif
408         
409         return -ENODEV;
410 }
411 
412 static void __pmac
413 media_bay_step(int i)
414 {
415         volatile struct media_bay_info* bay = &media_bays[i];
416 
417         /* We don't poll when powering down */
418         if (bay->state != mb_powering_down)
419             poll_media_bay(i);
420 
421         /* If timer expired or polling IDE busy, run state machine */
422         if ((bay->state != mb_ide_waiting) && (bay->timer != 0) && ((--bay->timer) != 0))
423             return;
424 
425         switch(bay->state) {
426         case mb_powering_up:
427                 set_media_bay(i, bay->last_value);
428                 bay->timer = MS_TO_HZ(MB_RESET_DELAY);
429                 bay->state = mb_enabling_bay;
430                 MBDBG("mediabay%d: enabling (kind:%d)\n", i, bay->content_id);
431                 break;
432         case mb_enabling_bay:
433                 feature_clear(bay->dev_node, FEATURE_Mediabay_reset);
434                 bay->timer = MS_TO_HZ(MB_SETUP_DELAY);
435                 bay->state = mb_resetting;
436                 MBDBG("mediabay%d: waiting reset (kind:%d)\n", i, bay->content_id);
437                 break;
438             
439         case mb_resetting:
440                 if (bay->content_id != MB_CD) {
441                         MBDBG("mediabay%d: bay is up (kind:%d)\n", i, bay->content_id);
442                         bay->state = mb_up;
443                         break;
444                 }
445 #ifdef CONFIG_BLK_DEV_IDE
446                 MBDBG("mediabay%d: waiting IDE reset (kind:%d)\n", i, bay->content_id);
447                 if (bay->pismo)
448                         feature_clear(bay->dev_node, FEATURE_IDE0_reset);
449                 else
450                         feature_clear(bay->dev_node, FEATURE_IDE1_reset);
451                 bay->timer = MS_TO_HZ(MB_IDE_WAIT);
452                 bay->state = mb_ide_resetting;
453 #else
454                 printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)\n", i);
455                 set_mb_power(i, 0);
456 #endif // #ifdef CONFIG_BLK_DEV_IDE
457                 break;
458             
459 #ifdef CONFIG_BLK_DEV_IDE
460         case mb_ide_resetting:
461                 bay->timer = MS_TO_HZ(MB_IDE_TIMEOUT);
462                 bay->state = mb_ide_waiting;
463                 MBDBG("mediabay%d: waiting IDE ready (kind:%d)\n", i, bay->content_id);
464                 break;
465             
466         case mb_ide_waiting:
467                 if (bay->cd_base == 0) {
468                         bay->timer = 0;
469                         bay->state = mb_up;
470                         MBDBG("mediabay%d: up before IDE init\n", i);
471                         break;
472                 } else if (MB_IDE_READY(i)) {
473                         bay->timer = 0;
474                         bay->state = mb_up;
475                         if (bay->cd_index < 0) {
476                                 pmu_suspend();
477                                 bay->cd_index = ide_register(bay->cd_base, 0, bay->cd_irq);
478                                 pmu_resume();
479                         }
480                         if (bay->cd_index == -1) {
481                                 /* We eventually do a retry */
482                                 bay->cd_retry++;
483                                 printk("IDE register error\n");
484                                 set_mb_power(i, 0);
485                         } else {
486                                 printk(KERN_DEBUG "media-bay %d is ide %d\n", i, bay->cd_index);
487                                 MBDBG("mediabay %d IDE ready\n", i);
488                         }
489                         break;
490                 }
491                 if (bay->timer == 0) {
492                         printk("\nIDE Timeout in bay %d !\n", i);
493                         MBDBG("mediabay%d: nIDE Timeout !\n", i);
494                         set_mb_power(i, 0);
495                 }
496                 break;
497 #endif // #ifdef CONFIG_BLK_DEV_IDE
498 
499         case mb_powering_down:
500                 bay->state = mb_empty;
501 #ifdef CONFIG_BLK_DEV_IDE
502                 if (bay->cd_index >= 0) {
503                         printk(KERN_DEBUG "Unregistering mb %d ide, index:%d\n", i,
504                                bay->cd_index);
505                         ide_unregister(bay->cd_index);
506                         bay->cd_index = -1;
507                 }
508                 if (bay->cd_retry) {
509                         if (bay->cd_retry > MAX_CD_RETRIES) {
510                                 /* Should add an error sound (sort of beep in dmasound) */
511                                 printk("\nmedia-bay %d, IDE device badly inserted or unrecognised\n", i);
512                         } else {
513                                 /* Force a new power down/up sequence */
514                                 bay->content_id = MB_NO;
515                         }
516                 }
517 #endif      
518                 MBDBG("mediabay%d: end of power down\n", i);
519                 break;
520         }
521 }
522 
523 /*
524  * This procedure runs as a kernel thread to poll the media bay
525  * once each tick and register and unregister the IDE interface
526  * with the IDE driver.  It needs to be a thread because
527  * ide_register can't be called from interrupt context.
528  */
529 int __pmac
530 media_bay_task(void *x)
531 {
532         int     i;
533 
534         strcpy(current->comm, "media-bay");
535 #ifdef MB_IGNORE_SIGNALS
536         sigfillset(&current->blocked);
537 #endif
538 
539         for (;;) {
540                 for (i = 0; i < media_bay_count; ++i)
541                         media_bay_step(i);
542 
543                 current->state = TASK_INTERRUPTIBLE;
544                 schedule_timeout(1);
545                 if (signal_pending(current))
546                         return 0;
547         }
548 }
549 
550 void __pmac
551 poll_media_bay(int which)
552 {
553         volatile struct media_bay_info* bay = &media_bays[which];
554         int id = mb_content(bay);
555 
556         if (id == bay->last_value) {
557             if (id != bay->content_id
558                 && ++bay->value_count >= MS_TO_HZ(MB_STABLE_DELAY)) {
559                 /* If the device type changes without going thru "MB_NO", we force
560                    a pass by "MB_NO" to make sure things are properly reset */
561                 if ((id != MB_NO) && (bay->content_id != MB_NO)) {
562                     id = MB_NO;
563                     MBDBG("mediabay%d: forcing MB_NO\n", which);
564                 }
565                 MBDBG("mediabay%d: switching to %d\n", which, id);
566                 set_mb_power(which, id != MB_NO);
567                 bay->content_id = id;
568                 if (id == MB_NO) {
569 #ifdef CONFIG_BLK_DEV_IDE
570                     bay->cd_retry = 0;
571 #endif
572                     printk(KERN_INFO "media bay %d is empty\n", which);
573                 }
574             }
575         } else {
576                 bay->last_value = id;
577                 bay->value_count = 0;
578         }
579 }
580 
581 
582 #ifdef CONFIG_PMAC_PBOOK
583 /*
584  * notify clients before sleep and reset bus afterwards
585  */
586 int __pmac
587 mb_notify_sleep(struct pmu_sleep_notifier *self, int when)
588 {
589         volatile struct media_bay_info* bay;
590         int i;
591         
592         switch (when) {
593         case PBOOK_SLEEP_REQUEST:
594         case PBOOK_SLEEP_REJECT:
595                 break;
596                 
597         case PBOOK_SLEEP_NOW:
598                 for (i=0; i<media_bay_count; i++) {
599                         bay = &media_bays[i];
600                         set_mb_power(i, 0);
601                         mdelay(10);
602                 }
603                 break;
604         case PBOOK_WAKE:
605                 for (i=0; i<media_bay_count; i++) {
606                         bay = &media_bays[i];
607                         /* We re-enable the bay using it's previous content
608                            only if it did not change. Note those bozo timings,
609                            they seem to help the 3400 get it right.
610                          */
611                         /* Force MB power to 0 */
612                         set_mb_power(i, 0);
613                         mdelay(MB_POWER_DELAY);
614                         if (!bay->pismo)
615                                 out_8(&bay->addr->contents, 0x70);
616                         mdelay(MB_STABLE_DELAY);
617                         if (mb_content(bay) != bay->content_id)
618                                 continue;
619                         set_mb_power(i, 1);
620                         bay->last_value = bay->content_id;
621                         bay->value_count = MS_TO_HZ(MB_STABLE_DELAY);
622                         bay->timer = MS_TO_HZ(MB_POWER_DELAY);
623 #ifdef CONFIG_BLK_DEV_IDE
624                         bay->cd_retry = 0;
625 #endif
626                         do {
627                                 mdelay(1000/HZ);
628                                 media_bay_step(i);
629                         } while((media_bays[i].state != mb_empty) &&
630                                 (media_bays[i].state != mb_up));
631                 }
632                 break;
633         }
634         return PBOOK_SLEEP_OK;
635 }
636 #endif /* CONFIG_PMAC_PBOOK */
637 
638 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.