mediabay.c 19 KB

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