mediabay.c 19 KB

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