mediabay.c 22 KB

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