mediabay.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:21k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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. #include <linux/config.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/delay.h>
  19. #include <linux/sched.h>
  20. #include <linux/timer.h>
  21. #include <linux/hdreg.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <asm/prom.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/io.h>
  27. #include <asm/machdep.h>
  28. #include <asm/pmac_feature.h>
  29. #include <asm/mediabay.h>
  30. #include <asm/sections.h>
  31. #include <asm/ohare.h>
  32. #include <asm/heathrow.h>
  33. #include <asm/keylargo.h>
  34. #include <linux/adb.h>
  35. #include <linux/pmu.h>
  36. #ifdef CONFIG_PMAC_PBOOK
  37. static int mb_notify_sleep(struct pmu_sleep_notifier *self, int when);
  38. static struct pmu_sleep_notifier mb_sleep_notifier = {
  39. mb_notify_sleep,
  40. SLEEP_LEVEL_MEDIABAY,
  41. };
  42. #endif
  43. #undef MB_USE_INTERRUPTS
  44. #define MB_DEBUG
  45. #define MB_IGNORE_SIGNALS
  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. /* Type of media bay */
  52. enum {
  53. mb_ohare,
  54. mb_heathrow,
  55. mb_keylargo
  56. };
  57. #define MB_FCR32(bay, r) ((bay)->base + ((r) >> 2))
  58. #define MB_FCR8(bay, r) (((volatile u8*)((bay)->base)) + (r))
  59. #define MB_IN32(bay,r) (in_le32(MB_FCR32(bay,r)))
  60. #define MB_OUT32(bay,r,v) (out_le32(MB_FCR32(bay,r), (v)))
  61. #define MB_BIS(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) | (v)))
  62. #define MB_BIC(bay,r,v) (MB_OUT32((bay), (r), MB_IN32((bay), r) & ~(v)))
  63. #define MB_IN8(bay,r) (in_8(MB_FCR8(bay,r)))
  64. #define MB_OUT8(bay,r,v) (out_8(MB_FCR8(bay,r), (v)))
  65. struct media_bay_info;
  66. struct mb_ops {
  67. char* name;
  68. u8 (*content)(struct media_bay_info* bay);
  69. void (*power)(struct media_bay_info* bay, int on_off);
  70. int (*setup_bus)(struct media_bay_info* bay, u8 device_id);
  71. void (*un_reset)(struct media_bay_info* bay);
  72. void (*un_reset_ide)(struct media_bay_info* bay);
  73. };
  74. struct media_bay_info {
  75. volatile u32* base;
  76. int content_id;
  77. int state;
  78. int last_value;
  79. int value_count;
  80. int timer;
  81. struct device_node* dev_node;
  82. int mb_type;
  83. struct mb_ops* ops;
  84. int index;
  85. int cached_gpio;
  86. #ifdef CONFIG_BLK_DEV_IDE
  87. unsigned long cd_base;
  88. int  cd_index;
  89. int cd_irq;
  90. int cd_retry;
  91. #endif
  92. };
  93. #define MAX_BAYS 2
  94. static struct media_bay_info media_bays[MAX_BAYS];
  95. int media_bay_count = 0;
  96. #ifdef CONFIG_BLK_DEV_IDE
  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) ((inb(media_bays[i].cd_base + 0x70) & 0x80) == 0)
  100. #endif
  101. /* Note: All delays are not in milliseconds and converted to HZ relative
  102.  * values by the macro below
  103.  */
  104. #define MS_TO_HZ(ms) ((ms * HZ) / 1000)
  105. /*
  106.  * Consider the media-bay ID value stable if it is the same for
  107.  * this number of milliseconds
  108.  */
  109. #define MB_STABLE_DELAY 100
  110. /* Wait after powering up the media bay this delay in ms
  111.  * timeout bumped for some powerbooks
  112.  */
  113. #define MB_POWER_DELAY 200
  114. /*
  115.  * Hold the media-bay reset signal true for this many ticks
  116.  * after a device is inserted before releasing it.
  117.  */
  118. #define MB_RESET_DELAY 40
  119. /*
  120.  * Wait this long after the reset signal is released and before doing
  121.  * further operations. After this delay, the IDE reset signal is released
  122.  * too for an IDE device
  123.  */
  124. #define MB_SETUP_DELAY 100
  125. /*
  126.  * Wait this many ticks after an IDE device (e.g. CD-ROM) is inserted
  127.  * (or until the device is ready) before waiting for busy bit to disappear
  128.  */
  129. #define MB_IDE_WAIT 1000
  130. /*
  131.  * Timeout waiting for busy bit of an IDE device to go down
  132.  */
  133. #define MB_IDE_TIMEOUT 5000
  134. /*
  135.  * Max retries of the full power up/down sequence for an IDE device
  136.  */
  137. #define MAX_CD_RETRIES 3
  138. /*
  139.  * States of a media bay
  140.  */
  141. enum {
  142. mb_empty = 0, /* Idle */
  143. mb_powering_up, /* power bit set, waiting MB_POWER_DELAY */
  144. mb_enabling_bay, /* enable bits set, waiting MB_RESET_DELAY */
  145. mb_resetting, /* reset bit unset, waiting MB_SETUP_DELAY */
  146. mb_ide_resetting, /* IDE reset bit unser, waiting MB_IDE_WAIT */
  147. mb_ide_waiting, /* Waiting for BUSY bit to go away until MB_IDE_TIMEOUT */
  148. mb_up, /* Media bay full */
  149. mb_powering_down /* Powering down (avoid too fast down/up) */
  150. };
  151. #define MB_POWER_SOUND 0x08
  152. #define MB_POWER_FLOPPY 0x04
  153. #define MB_POWER_ATA 0x02
  154. #define MB_POWER_PCI 0x01
  155. #define MB_POWER_OFF 0x00
  156. /*
  157.  * Functions for polling content of media bay
  158.  */
  159.  
  160. static u8 __pmac
  161. ohare_mb_content(struct media_bay_info *bay)
  162. {
  163. return (MB_IN32(bay, OHARE_MBCR) >> 12) & 7;
  164. }
  165. static u8 __pmac
  166. heathrow_mb_content(struct media_bay_info *bay)
  167. {
  168. return (MB_IN32(bay, HEATHROW_MBCR) >> 12) & 7;
  169. }
  170. static u8 __pmac
  171. keylargo_mb_content(struct media_bay_info *bay)
  172. {
  173. int new_gpio;
  174. new_gpio = MB_IN8(bay, KL_GPIO_MEDIABAY_IRQ) & KEYLARGO_GPIO_INPUT_DATA;
  175. if (new_gpio) {
  176. bay->cached_gpio = new_gpio;
  177. return MB_NO;
  178. } else if (bay->cached_gpio != new_gpio) {
  179. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
  180. (void)MB_IN32(bay, KEYLARGO_MBCR);
  181. udelay(5);
  182. MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
  183. (void)MB_IN32(bay, KEYLARGO_MBCR);
  184. udelay(5);
  185. bay->cached_gpio = new_gpio;
  186. }
  187. return (MB_IN32(bay, KEYLARGO_MBCR) >> 4) & 7;
  188. }
  189. /*
  190.  * Functions for powering up/down the bay, puts the bay device
  191.  * into reset state as well
  192.  */
  193. static void __pmac
  194. ohare_mb_power(struct media_bay_info* bay, int on_off)
  195. {
  196. if (on_off) {
  197. /* Power up device, assert it's reset line */
  198. MB_BIC(bay, OHARE_FCR, OH_BAY_RESET_N);
  199. MB_BIC(bay, OHARE_FCR, OH_BAY_POWER_N);
  200. } else {
  201. /* Disable all devices */
  202. MB_BIC(bay, OHARE_FCR, OH_BAY_DEV_MASK);
  203. MB_BIC(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
  204. /* Cut power from bay, release reset line */
  205. MB_BIS(bay, OHARE_FCR, OH_BAY_POWER_N);
  206. MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
  207. MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
  208. }
  209. MB_BIC(bay, OHARE_MBCR, 0x00000F00);
  210. }
  211. static void __pmac
  212. heathrow_mb_power(struct media_bay_info* bay, int on_off)
  213. {
  214. if (on_off) {
  215. /* Power up device, assert it's reset line */
  216. MB_BIC(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
  217. MB_BIC(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
  218. } else {
  219. /* Disable all devices */
  220. MB_BIC(bay, HEATHROW_FCR, HRW_BAY_DEV_MASK);
  221. MB_BIC(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
  222. /* Cut power from bay, release reset line */
  223. MB_BIS(bay, HEATHROW_FCR, HRW_BAY_POWER_N);
  224. MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
  225. MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
  226. }
  227. MB_BIC(bay, HEATHROW_MBCR, 0x00000F00);
  228. }
  229. static void __pmac
  230. keylargo_mb_power(struct media_bay_info* bay, int on_off)
  231. {
  232. if (on_off) {
  233. /* Power up device, assert it's reset line */
  234.              MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
  235.              MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
  236. } else {
  237. /* Disable all devices */
  238. MB_BIC(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
  239. MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
  240. /* Cut power from bay, release reset line */
  241. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_POWER);
  242. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
  243. MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
  244. }
  245. MB_BIC(bay, KEYLARGO_MBCR, 0x0000000F);
  246. }
  247. /*
  248.  * Functions for configuring the media bay for a given type of device,
  249.  * enable the related busses
  250.  */
  251. static int __pmac
  252. ohare_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
  253. {
  254. switch(device_id) {
  255. case MB_FD:
  256. case MB_FD1:
  257. MB_BIS(bay, OHARE_FCR, OH_BAY_FLOPPY_ENABLE);
  258. MB_BIS(bay, OHARE_FCR, OH_FLOPPY_ENABLE);
  259. return 0;
  260. case MB_CD:
  261. MB_BIC(bay, OHARE_FCR, OH_IDE1_RESET_N);
  262. MB_BIS(bay, OHARE_FCR, OH_BAY_IDE_ENABLE);
  263. return 0;
  264. case MB_PCI:
  265. MB_BIS(bay, OHARE_FCR, OH_BAY_PCI_ENABLE);
  266. return 0;
  267. }
  268. return -ENODEV;
  269. }
  270. static int __pmac
  271. heathrow_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
  272. {
  273. switch(device_id) {
  274. case MB_FD:
  275. case MB_FD1:
  276. MB_BIS(bay, HEATHROW_FCR, HRW_BAY_FLOPPY_ENABLE);
  277. MB_BIS(bay, HEATHROW_FCR, HRW_SWIM_ENABLE);
  278. return 0;
  279. case MB_CD:
  280. MB_BIC(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
  281. MB_BIS(bay, HEATHROW_FCR, HRW_BAY_IDE_ENABLE);
  282. return 0;
  283. case MB_PCI:
  284. MB_BIS(bay, HEATHROW_FCR, HRW_BAY_PCI_ENABLE);
  285. return 0;
  286. }
  287. return -ENODEV;
  288. }
  289. static int __pmac
  290. keylargo_mb_setup_bus(struct media_bay_info* bay, u8 device_id)
  291. {
  292. switch(device_id) {
  293. case MB_CD:
  294. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
  295. MB_BIC(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
  296. MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_ENABLE);
  297. return 0;
  298. case MB_PCI:
  299. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_PCI_ENABLE);
  300. return 0;
  301. case MB_SOUND:
  302. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_SOUND_ENABLE);
  303. return 0;
  304. }
  305. return -ENODEV;
  306. }
  307. /*
  308.  * Functions for tweaking resets
  309.  */
  310. static void __pmac
  311. ohare_mb_un_reset(struct media_bay_info* bay)
  312. {
  313. MB_BIS(bay, OHARE_FCR, OH_BAY_RESET_N);
  314. }
  315. static void __pmac
  316. heathrow_mb_un_reset(struct media_bay_info* bay)
  317. {
  318. MB_BIS(bay, HEATHROW_FCR, HRW_BAY_RESET_N);
  319. }
  320. static void __pmac
  321. keylargo_mb_un_reset(struct media_bay_info* bay)
  322. {
  323. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_DEV_RESET);
  324. }
  325. static void __pmac
  326. ohare_mb_un_reset_ide(struct media_bay_info* bay)
  327. {
  328. MB_BIS(bay, OHARE_FCR, OH_IDE1_RESET_N);
  329. }
  330. static void __pmac
  331. heathrow_mb_un_reset_ide(struct media_bay_info* bay)
  332. {
  333. MB_BIS(bay, HEATHROW_FCR, HRW_IDE1_RESET_N);
  334. }
  335. static void __pmac
  336. keylargo_mb_un_reset_ide(struct media_bay_info* bay)
  337. {
  338. MB_BIS(bay, KEYLARGO_FCR1, KL1_EIDE0_RESET_N);
  339. }
  340. static inline void __pmac
  341. set_mb_power(struct media_bay_info* bay, int onoff)
  342. {
  343. /* Power up up and assert the bay reset line */
  344. if (onoff) {
  345. bay->ops->power(bay, 1);
  346. bay->state = mb_powering_up;
  347. MBDBG("mediabay%d: powering upn", bay->index);
  348. } else { 
  349. /* Make sure everything is powered down & disabled */
  350. bay->ops->power(bay, 0);
  351. bay->state = mb_powering_down;
  352. MBDBG("mediabay%d: powering downn", bay->index);
  353. }
  354. bay->timer = MS_TO_HZ(MB_POWER_DELAY);
  355. }
  356. static void __pmac
  357. poll_media_bay(struct media_bay_info* bay)
  358. {
  359. int id = bay->ops->content(bay);
  360. if (id == bay->last_value) {
  361.     if (id != bay->content_id
  362.         && ++bay->value_count >= MS_TO_HZ(MB_STABLE_DELAY)) {
  363.         /* If the device type changes without going thru "MB_NO", we force
  364.            a pass by "MB_NO" to make sure things are properly reset */
  365.         if ((id != MB_NO) && (bay->content_id != MB_NO)) {
  366.             id = MB_NO;
  367.     MBDBG("mediabay%d: forcing MB_NOn", bay->index);
  368. }
  369. MBDBG("mediabay%d: switching to %dn", bay->index, id);
  370. set_mb_power(bay, id != MB_NO);
  371. bay->content_id = id;
  372. if (id == MB_NO) {
  373. #ifdef CONFIG_BLK_DEV_IDE
  374.     bay->cd_retry = 0;
  375. #endif
  376.     printk(KERN_INFO "media bay %d is emptyn", bay->index);
  377. }
  378.       }
  379. } else {
  380. bay->last_value = id;
  381. bay->value_count = 0;
  382. }
  383. }
  384. int __pmac
  385. check_media_bay(struct device_node *which_bay, int what)
  386. {
  387. #ifdef CONFIG_BLK_DEV_IDE
  388. int i;
  389. for (i=0; i<media_bay_count; i++)
  390. if (which_bay == media_bays[i].dev_node)
  391. {
  392. if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
  393. return 0;
  394. media_bays[i].cd_index = -1;
  395. return -EINVAL;
  396. }
  397. #endif /* CONFIG_BLK_DEV_IDE */
  398. return -ENODEV;
  399. }
  400. int __pmac
  401. check_media_bay_by_base(unsigned long base, int what)
  402. {
  403. #ifdef CONFIG_BLK_DEV_IDE
  404. int i;
  405. for (i=0; i<media_bay_count; i++)
  406. if (base == media_bays[i].cd_base)
  407. {
  408. if ((what == media_bays[i].content_id) && media_bays[i].state == mb_up)
  409. return 0;
  410. media_bays[i].cd_index = -1;
  411. return -EINVAL;
  412. #endif
  413. return -ENODEV;
  414. }
  415. int __pmac
  416. media_bay_set_ide_infos(struct device_node* which_bay, unsigned long base,
  417. int irq, int index)
  418. {
  419. #ifdef CONFIG_BLK_DEV_IDE
  420. int i;
  421. for (i=0; i<media_bay_count; i++)
  422. if (which_bay == media_bays[i].dev_node)
  423. {
  424. int timeout = 5000;
  425.   media_bays[i].cd_base = base;
  426. media_bays[i].cd_irq = irq;
  427. if ((MB_CD != media_bays[i].content_id) || media_bays[i].state != mb_up)
  428. return 0;
  429. printk(KERN_DEBUG "Registered ide %d for media bay %dn", index, i);
  430. do {
  431. if (MB_IDE_READY(i)) {
  432. media_bays[i].cd_index = index;
  433. return 0;
  434. }
  435. mdelay(1);
  436. } while(--timeout);
  437. printk(KERN_DEBUG "Timeount waiting IDE in bay %dn", i);
  438. return -ENODEV;
  439. #endif
  440. return -ENODEV;
  441. }
  442. static void __pmac
  443. media_bay_step(int i)
  444. {
  445. struct media_bay_info* bay = &media_bays[i];
  446. /* We don't poll when powering down */
  447. if (bay->state != mb_powering_down)
  448.     poll_media_bay(bay);
  449. /* If timer expired or polling IDE busy, run state machine */
  450. if ((bay->state != mb_ide_waiting) && (bay->timer != 0) && ((--bay->timer) != 0))
  451.     return;
  452. switch(bay->state) {
  453. case mb_powering_up:
  454.      if (bay->ops->setup_bus(bay, bay->last_value) < 0) {
  455. MBDBG("mediabay%d: device not supported (kind:%d)n", i, bay->content_id);
  456.      set_mb_power(bay, 0);
  457.      break;
  458.      }
  459.      bay->timer = MS_TO_HZ(MB_RESET_DELAY);
  460.      bay->state = mb_enabling_bay;
  461. MBDBG("mediabay%d: enabling (kind:%d)n", i, bay->content_id);
  462. break;
  463. case mb_enabling_bay:
  464. bay->ops->un_reset(bay);
  465.      bay->timer = MS_TO_HZ(MB_SETUP_DELAY);
  466.      bay->state = mb_resetting;
  467. MBDBG("mediabay%d: waiting reset (kind:%d)n", i, bay->content_id);
  468.      break;
  469.     
  470. case mb_resetting:
  471. if (bay->content_id != MB_CD) {
  472. MBDBG("mediabay%d: bay is up (kind:%d)n", i, bay->content_id);
  473. bay->state = mb_up;
  474. break;
  475.      }
  476. #ifdef CONFIG_BLK_DEV_IDE
  477. MBDBG("mediabay%d: waiting IDE reset (kind:%d)n", i, bay->content_id);
  478. bay->ops->un_reset_ide(bay);
  479.      bay->timer = MS_TO_HZ(MB_IDE_WAIT);
  480.      bay->state = mb_ide_resetting;
  481. #else
  482. printk(KERN_DEBUG "media-bay %d is ide (not compiled in kernel)n", i);
  483. set_mb_power(bay, 0);
  484. #endif /* CONFIG_BLK_DEV_IDE */
  485.      break;
  486.     
  487. #ifdef CONFIG_BLK_DEV_IDE
  488. case mb_ide_resetting:
  489.      bay->timer = MS_TO_HZ(MB_IDE_TIMEOUT);
  490.      bay->state = mb_ide_waiting;
  491. MBDBG("mediabay%d: waiting IDE ready (kind:%d)n", i, bay->content_id);
  492.      break;
  493.     
  494. case mb_ide_waiting:
  495.      if (bay->cd_base == 0) {
  496. bay->timer = 0;
  497. bay->state = mb_up;
  498. MBDBG("mediabay%d: up before IDE initn", i);
  499. break;
  500.      } else if (MB_IDE_READY(i)) {
  501. bay->timer = 0;
  502. bay->state = mb_up;
  503. if (bay->cd_index < 0) {
  504. pmu_suspend();
  505. bay->cd_index = ide_register(bay->cd_base, 0, bay->cd_irq);
  506. pmu_resume();
  507. }
  508. if (bay->cd_index == -1) {
  509. /* We eventually do a retry */
  510. bay->cd_retry++;
  511. printk("IDE register errorn");
  512. set_mb_power(bay, 0);
  513. } else {
  514. printk(KERN_DEBUG "media-bay %d is ide %dn", i, bay->cd_index);
  515. MBDBG("mediabay %d IDE readyn", i);
  516. }
  517. break;
  518.      }
  519.      if (bay->timer == 0) {
  520. printk("nIDE Timeout in bay %d !n", i);
  521. MBDBG("mediabay%d: nIDE Timeout !n", i);
  522. set_mb_power(bay, 0);
  523.      }
  524. break;
  525. #endif /* CONFIG_BLK_DEV_IDE */
  526. case mb_powering_down:
  527.      bay->state = mb_empty;
  528. #ifdef CONFIG_BLK_DEV_IDE
  529.              if (bay->cd_index >= 0) {
  530. printk(KERN_DEBUG "Unregistering mb %d ide, index:%dn", i,
  531.        bay->cd_index);
  532. ide_unregister(bay->cd_index);
  533. bay->cd_index = -1;
  534. }
  535.      if (bay->cd_retry) {
  536. if (bay->cd_retry > MAX_CD_RETRIES) {
  537. /* Should add an error sound (sort of beep in dmasound) */
  538. printk("nmedia-bay %d, IDE device badly inserted or unrecognisedn", i);
  539. } else {
  540. /* Force a new power down/up sequence */
  541. bay->content_id = MB_NO;
  542. }
  543.      }
  544. #endif /* CONFIG_BLK_DEV_IDE */    
  545. MBDBG("mediabay%d: end of power downn", i);
  546.      break;
  547. }
  548. }
  549. /*
  550.  * This procedure runs as a kernel thread to poll the media bay
  551.  * once each tick and register and unregister the IDE interface
  552.  * with the IDE driver.  It needs to be a thread because
  553.  * ide_register can't be called from interrupt context.
  554.  */
  555. static int __pmac
  556. media_bay_task(void *x)
  557. {
  558. int i;
  559. strcpy(current->comm, "media-bay");
  560. #ifdef MB_IGNORE_SIGNALS
  561. sigfillset(&current->blocked);
  562. #endif
  563. for (;;) {
  564. for (i = 0; i < media_bay_count; ++i)
  565. media_bay_step(i);
  566. current->state = TASK_INTERRUPTIBLE;
  567. schedule_timeout(1);
  568. if (signal_pending(current))
  569. return 0;
  570. }
  571. }
  572. #ifdef MB_USE_INTERRUPTS
  573. static void __pmac
  574. media_bay_intr(int irq, void *devid, struct pt_regs *regs)
  575. {
  576. }
  577. #endif
  578. #ifdef CONFIG_PMAC_PBOOK
  579. /*
  580.  * notify clients before sleep and reset bus afterwards
  581.  */
  582. int __pmac
  583. mb_notify_sleep(struct pmu_sleep_notifier *self, int when)
  584. {
  585. struct media_bay_info* bay;
  586. int i;
  587. switch (when) {
  588. case PBOOK_SLEEP_REQUEST:
  589. case PBOOK_SLEEP_REJECT:
  590. break;
  591. case PBOOK_SLEEP_NOW:
  592. for (i=0; i<media_bay_count; i++) {
  593. bay = &media_bays[i];
  594. set_mb_power(bay, 0);
  595. mdelay(10);
  596. }
  597. break;
  598. case PBOOK_WAKE:
  599. for (i=0; i<media_bay_count; i++) {
  600. bay = &media_bays[i];
  601. /* We re-enable the bay using it's previous content
  602.    only if it did not change. Note those bozo timings,
  603.    they seem to help the 3400 get it right.
  604.  */
  605. /* Force MB power to 0 */
  606. set_mb_power(bay, 0);
  607. mdelay(MB_POWER_DELAY);
  608. if (bay->ops->content(bay) != bay->content_id)
  609. continue;
  610. set_mb_power(bay, 1);
  611. bay->last_value = bay->content_id;
  612. bay->value_count = MS_TO_HZ(MB_STABLE_DELAY);
  613. bay->timer = MS_TO_HZ(MB_POWER_DELAY);
  614. #ifdef CONFIG_BLK_DEV_IDE
  615. bay->cd_retry = 0;
  616. #endif
  617. do {
  618. mdelay(1000/HZ);
  619. media_bay_step(i);
  620. } while((media_bays[i].state != mb_empty) &&
  621. (media_bays[i].state != mb_up));
  622. }
  623. break;
  624. }
  625. return PBOOK_SLEEP_OK;
  626. }
  627. #endif /* CONFIG_PMAC_PBOOK */
  628. /* Definitions of "ops" structures.
  629.  */
  630. static struct mb_ops ohare_mb_ops __pmacdata = {
  631. name: "Ohare",
  632. content: ohare_mb_content,
  633. power: ohare_mb_power,
  634. setup_bus: ohare_mb_setup_bus,
  635. un_reset: ohare_mb_un_reset,
  636. un_reset_ide: ohare_mb_un_reset_ide,
  637. };
  638. static struct mb_ops heathrow_mb_ops __pmacdata = {
  639. name: "Heathrow",
  640. content: heathrow_mb_content,
  641. power: heathrow_mb_power,
  642. setup_bus: heathrow_mb_setup_bus,
  643. un_reset: heathrow_mb_un_reset,
  644. un_reset_ide: heathrow_mb_un_reset_ide,
  645. };
  646. static struct mb_ops keylargo_mb_ops __pmacdata = {
  647. name: "KeyLargo",
  648. content: keylargo_mb_content,
  649. power: keylargo_mb_power,
  650. setup_bus: keylargo_mb_setup_bus,
  651. un_reset: keylargo_mb_un_reset,
  652. un_reset_ide: keylargo_mb_un_reset_ide,
  653. };
  654. /*
  655.  * It seems that the bit for the media-bay interrupt in the IRQ_LEVEL
  656.  * register is always set when there is something in the media bay.
  657.  * This causes problems for the interrupt code if we attach an interrupt
  658.  * handler to the media-bay interrupt, because it tends to go into
  659.  * an infinite loop calling the media bay interrupt handler.
  660.  * Therefore we do it all by polling the media bay once each tick.
  661.  */
  662. void __pmac
  663. media_bay_init(void)
  664. {
  665. struct device_node *np;
  666. int n,i;
  667. for (i=0; i<MAX_BAYS; i++) {
  668. memset((char *)&media_bays[i], 0, sizeof(struct media_bay_info));
  669. media_bays[i].content_id = -1;
  670. #ifdef CONFIG_BLK_DEV_IDE
  671. media_bays[i].cd_index = -1;
  672. #endif
  673. }
  674. np = find_devices("media-bay");
  675. n = 0;
  676. while(np && (n<MAX_BAYS)) {
  677. struct media_bay_info* bay = &media_bays[n];
  678. if (!np->parent || np->n_addrs == 0 || !request_OF_resource(np, 0, NULL)) {
  679. np = np->next;
  680. printk(KERN_ERR "media-bay: Can't request IO resource !n");
  681. continue;
  682. }
  683. bay->mb_type = mb_ohare;
  684. if (device_is_compatible(np, "keylargo-media-bay")) {
  685. bay->mb_type = mb_keylargo;
  686. bay->ops = &keylargo_mb_ops;
  687. } else if (device_is_compatible(np, "heathrow-media-bay")) {
  688. bay->mb_type = mb_heathrow;
  689. bay->ops = &heathrow_mb_ops;
  690. } else if (device_is_compatible(np, "ohare-media-bay")) {
  691. bay->mb_type = mb_ohare;
  692. bay->ops = &ohare_mb_ops;
  693. } else {
  694. printk(KERN_ERR "mediabay: Unknown bay type !n");
  695. np = np->next;
  696. continue;
  697. }
  698. bay->base = (volatile u32*)ioremap(np->parent->addrs[0].address, 0x1000);
  699. /* Enable probe logic on keylargo */
  700. if (bay->mb_type == mb_keylargo)
  701. MB_BIS(bay, KEYLARGO_MBCR, KL_MBCR_MB0_ENABLE);
  702. #ifdef MB_USE_INTERRUPTS
  703. if (np->n_intrs == 0) {
  704. printk(KERN_ERR "media bay %d has no irqn",n);
  705. np = np->next;
  706. continue;
  707. }
  708. if (request_irq(np->intrs[0].line, media_bay_intr, 0, "Media bay", (void *)n)) {
  709. printk(KERN_ERR "Couldn't get IRQ %d for media bay %dn",
  710. np->intrs[0].line, n);
  711. np = np->next;
  712. continue;
  713. }
  714. #endif
  715. media_bay_count++;
  716. printk(KERN_INFO "mediabay%d: Registered %s media-bayn", n, bay->ops->name);
  717. bay->dev_node = np;
  718. bay->index = n;
  719. /* Force an immediate detect */
  720. set_mb_power(bay, 0);
  721. mdelay(MB_POWER_DELAY);
  722. bay->content_id = MB_NO;
  723. bay->last_value = bay->ops->content(bay);
  724. bay->value_count = MS_TO_HZ(MB_STABLE_DELAY);
  725. bay->state = mb_empty;
  726. do {
  727. mdelay(1000/HZ);
  728. media_bay_step(n);
  729. } while((bay->state != mb_empty) &&
  730. (bay->state != mb_up));
  731. n++;
  732. np=np->next;
  733. }
  734. if (media_bay_count)
  735. {
  736. #ifdef CONFIG_PMAC_PBOOK
  737. pmu_register_sleep_notifier(&mb_sleep_notifier);
  738. #endif /* CONFIG_PMAC_PBOOK */
  739. kernel_thread(media_bay_task, NULL,
  740.       CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  741. }
  742. }