gdrom.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /* GD ROM driver for the SEGA Dreamcast
  2. * copyright Adrian McMenamin, 2007
  3. * With thanks to Marcus Comstedt and Nathan Keynes
  4. * for work in reversing PIO and DMA
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/fs.h>
  25. #include <linux/kernel.h>
  26. #include <linux/list.h>
  27. #include <linux/slab.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/cdrom.h>
  30. #include <linux/genhd.h>
  31. #include <linux/bio.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/device.h>
  35. #include <linux/wait.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/platform_device.h>
  38. #include <scsi/scsi.h>
  39. #include <asm/io.h>
  40. #include <asm/dma.h>
  41. #include <asm/delay.h>
  42. #include <mach/dma.h>
  43. #include <mach/sysasic.h>
  44. #define GDROM_DEV_NAME "gdrom"
  45. #define GD_SESSION_OFFSET 150
  46. /* GD Rom commands */
  47. #define GDROM_COM_SOFTRESET 0x08
  48. #define GDROM_COM_EXECDIAG 0x90
  49. #define GDROM_COM_PACKET 0xA0
  50. #define GDROM_COM_IDDEV 0xA1
  51. /* GD Rom registers */
  52. #define GDROM_BASE_REG 0xA05F7000
  53. #define GDROM_ALTSTATUS_REG (GDROM_BASE_REG + 0x18)
  54. #define GDROM_DATA_REG (GDROM_BASE_REG + 0x80)
  55. #define GDROM_ERROR_REG (GDROM_BASE_REG + 0x84)
  56. #define GDROM_INTSEC_REG (GDROM_BASE_REG + 0x88)
  57. #define GDROM_SECNUM_REG (GDROM_BASE_REG + 0x8C)
  58. #define GDROM_BCL_REG (GDROM_BASE_REG + 0x90)
  59. #define GDROM_BCH_REG (GDROM_BASE_REG + 0x94)
  60. #define GDROM_DSEL_REG (GDROM_BASE_REG + 0x98)
  61. #define GDROM_STATUSCOMMAND_REG (GDROM_BASE_REG + 0x9C)
  62. #define GDROM_RESET_REG (GDROM_BASE_REG + 0x4E4)
  63. #define GDROM_DMA_STARTADDR_REG (GDROM_BASE_REG + 0x404)
  64. #define GDROM_DMA_LENGTH_REG (GDROM_BASE_REG + 0x408)
  65. #define GDROM_DMA_DIRECTION_REG (GDROM_BASE_REG + 0x40C)
  66. #define GDROM_DMA_ENABLE_REG (GDROM_BASE_REG + 0x414)
  67. #define GDROM_DMA_STATUS_REG (GDROM_BASE_REG + 0x418)
  68. #define GDROM_DMA_WAIT_REG (GDROM_BASE_REG + 0x4A0)
  69. #define GDROM_DMA_ACCESS_CTRL_REG (GDROM_BASE_REG + 0x4B8)
  70. #define GDROM_HARD_SECTOR 2048
  71. #define BLOCK_LAYER_SECTOR 512
  72. #define GD_TO_BLK 4
  73. #define GDROM_DEFAULT_TIMEOUT (HZ * 7)
  74. static const struct {
  75. int sense_key;
  76. const char * const text;
  77. } sense_texts[] = {
  78. {NO_SENSE, "OK"},
  79. {RECOVERED_ERROR, "Recovered from error"},
  80. {NOT_READY, "Device not ready"},
  81. {MEDIUM_ERROR, "Disk not ready"},
  82. {HARDWARE_ERROR, "Hardware error"},
  83. {ILLEGAL_REQUEST, "Command has failed"},
  84. {UNIT_ATTENTION, "Device needs attention - disk may have been changed"},
  85. {DATA_PROTECT, "Data protection error"},
  86. {ABORTED_COMMAND, "Command aborted"},
  87. };
  88. static struct platform_device *pd;
  89. static int gdrom_major;
  90. static DECLARE_WAIT_QUEUE_HEAD(command_queue);
  91. static DECLARE_WAIT_QUEUE_HEAD(request_queue);
  92. static DEFINE_SPINLOCK(gdrom_lock);
  93. static void gdrom_readdisk_dma(struct work_struct *work);
  94. static DECLARE_WORK(work, gdrom_readdisk_dma);
  95. static LIST_HEAD(gdrom_deferred);
  96. struct gdromtoc {
  97. unsigned int entry[99];
  98. unsigned int first, last;
  99. unsigned int leadout;
  100. };
  101. static struct gdrom_unit {
  102. struct gendisk *disk;
  103. struct cdrom_device_info *cd_info;
  104. int status;
  105. int pending;
  106. int transfer;
  107. char disk_type;
  108. struct gdromtoc *toc;
  109. struct request_queue *gdrom_rq;
  110. } gd;
  111. struct gdrom_id {
  112. char mid;
  113. char modid;
  114. char verid;
  115. char padA[13];
  116. char mname[16];
  117. char modname[16];
  118. char firmver[16];
  119. char padB[16];
  120. };
  121. static int gdrom_getsense(short *bufstring);
  122. static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
  123. struct packet_command *command);
  124. static int gdrom_hardreset(struct cdrom_device_info *cd_info);
  125. static bool gdrom_is_busy(void)
  126. {
  127. return (ctrl_inb(GDROM_ALTSTATUS_REG) & 0x80) != 0;
  128. }
  129. static bool gdrom_data_request(void)
  130. {
  131. return (ctrl_inb(GDROM_ALTSTATUS_REG) & 0x88) == 8;
  132. }
  133. static bool gdrom_wait_clrbusy(void)
  134. {
  135. unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
  136. while ((ctrl_inb(GDROM_ALTSTATUS_REG) & 0x80) &&
  137. (time_before(jiffies, timeout)))
  138. cpu_relax();
  139. return time_before(jiffies, timeout + 1);
  140. }
  141. static bool gdrom_wait_busy_sleeps(void)
  142. {
  143. unsigned long timeout;
  144. /* Wait to get busy first */
  145. timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
  146. while (!gdrom_is_busy() && time_before(jiffies, timeout))
  147. cpu_relax();
  148. /* Now wait for busy to clear */
  149. return gdrom_wait_clrbusy();
  150. }
  151. static void gdrom_identifydevice(void *buf)
  152. {
  153. int c;
  154. short *data = buf;
  155. /* If the device won't clear it has probably
  156. * been hit by a serious failure - but we'll
  157. * try to return a sense key even so */
  158. if (!gdrom_wait_clrbusy()) {
  159. gdrom_getsense(NULL);
  160. return;
  161. }
  162. ctrl_outb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG);
  163. if (!gdrom_wait_busy_sleeps()) {
  164. gdrom_getsense(NULL);
  165. return;
  166. }
  167. /* now read in the data */
  168. for (c = 0; c < 40; c++)
  169. data[c] = ctrl_inw(GDROM_DATA_REG);
  170. }
  171. static void gdrom_spicommand(void *spi_string, int buflen)
  172. {
  173. short *cmd = spi_string;
  174. unsigned long timeout;
  175. /* ensure IRQ_WAIT is set */
  176. ctrl_outb(0x08, GDROM_ALTSTATUS_REG);
  177. /* specify how many bytes we expect back */
  178. ctrl_outb(buflen & 0xFF, GDROM_BCL_REG);
  179. ctrl_outb((buflen >> 8) & 0xFF, GDROM_BCH_REG);
  180. /* other parameters */
  181. ctrl_outb(0, GDROM_INTSEC_REG);
  182. ctrl_outb(0, GDROM_SECNUM_REG);
  183. ctrl_outb(0, GDROM_ERROR_REG);
  184. /* Wait until we can go */
  185. if (!gdrom_wait_clrbusy()) {
  186. gdrom_getsense(NULL);
  187. return;
  188. }
  189. timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
  190. ctrl_outb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
  191. while (!gdrom_data_request() && time_before(jiffies, timeout))
  192. cpu_relax();
  193. if (!time_before(jiffies, timeout + 1)) {
  194. gdrom_getsense(NULL);
  195. return;
  196. }
  197. outsw(GDROM_DATA_REG, cmd, 6);
  198. }
  199. /* gdrom_command_executediagnostic:
  200. * Used to probe for presence of working GDROM
  201. * Restarts GDROM device and then applies standard ATA 3
  202. * Execute Diagnostic Command: a return of '1' indicates device 0
  203. * present and device 1 absent
  204. */
  205. static char gdrom_execute_diagnostic(void)
  206. {
  207. gdrom_hardreset(gd.cd_info);
  208. if (!gdrom_wait_clrbusy())
  209. return 0;
  210. ctrl_outb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG);
  211. if (!gdrom_wait_busy_sleeps())
  212. return 0;
  213. return ctrl_inb(GDROM_ERROR_REG);
  214. }
  215. /*
  216. * Prepare disk command
  217. * byte 0 = 0x70
  218. * byte 1 = 0x1f
  219. */
  220. static int gdrom_preparedisk_cmd(void)
  221. {
  222. struct packet_command *spin_command;
  223. spin_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
  224. if (!spin_command)
  225. return -ENOMEM;
  226. spin_command->cmd[0] = 0x70;
  227. spin_command->cmd[2] = 0x1f;
  228. spin_command->buflen = 0;
  229. gd.pending = 1;
  230. gdrom_packetcommand(gd.cd_info, spin_command);
  231. /* 60 second timeout */
  232. wait_event_interruptible_timeout(command_queue, gd.pending == 0,
  233. GDROM_DEFAULT_TIMEOUT);
  234. gd.pending = 0;
  235. kfree(spin_command);
  236. if (gd.status & 0x01) {
  237. /* log an error */
  238. gdrom_getsense(NULL);
  239. return -EIO;
  240. }
  241. return 0;
  242. }
  243. /*
  244. * Read TOC command
  245. * byte 0 = 0x14
  246. * byte 1 = session
  247. * byte 3 = sizeof TOC >> 8 ie upper byte
  248. * byte 4 = sizeof TOC & 0xff ie lower byte
  249. */
  250. static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session)
  251. {
  252. int tocsize;
  253. struct packet_command *toc_command;
  254. int err = 0;
  255. toc_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
  256. if (!toc_command)
  257. return -ENOMEM;
  258. tocsize = sizeof(struct gdromtoc);
  259. toc_command->cmd[0] = 0x14;
  260. toc_command->cmd[1] = session;
  261. toc_command->cmd[3] = tocsize >> 8;
  262. toc_command->cmd[4] = tocsize & 0xff;
  263. toc_command->buflen = tocsize;
  264. if (gd.pending) {
  265. err = -EBUSY;
  266. goto cleanup_readtoc_final;
  267. }
  268. gd.pending = 1;
  269. gdrom_packetcommand(gd.cd_info, toc_command);
  270. wait_event_interruptible_timeout(command_queue, gd.pending == 0,
  271. GDROM_DEFAULT_TIMEOUT);
  272. if (gd.pending) {
  273. err = -EINVAL;
  274. goto cleanup_readtoc;
  275. }
  276. insw(GDROM_DATA_REG, toc, tocsize/2);
  277. if (gd.status & 0x01)
  278. err = -EINVAL;
  279. cleanup_readtoc:
  280. gd.pending = 0;
  281. cleanup_readtoc_final:
  282. kfree(toc_command);
  283. return err;
  284. }
  285. /* TOC helpers */
  286. static int get_entry_lba(int track)
  287. {
  288. return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET);
  289. }
  290. static int get_entry_q_ctrl(int track)
  291. {
  292. return (track & 0x000000f0) >> 4;
  293. }
  294. static int get_entry_track(int track)
  295. {
  296. return (track & 0x0000ff00) >> 8;
  297. }
  298. static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
  299. struct cdrom_multisession *ms_info)
  300. {
  301. int fentry, lentry, track, data, tocuse, err;
  302. if (!gd.toc)
  303. return -ENOMEM;
  304. tocuse = 1;
  305. /* Check if GD-ROM */
  306. err = gdrom_readtoc_cmd(gd.toc, 1);
  307. /* Not a GD-ROM so check if standard CD-ROM */
  308. if (err) {
  309. tocuse = 0;
  310. err = gdrom_readtoc_cmd(gd.toc, 0);
  311. if (err) {
  312. pr_info("Could not get CD table of contents\n");
  313. return -ENXIO;
  314. }
  315. }
  316. fentry = get_entry_track(gd.toc->first);
  317. lentry = get_entry_track(gd.toc->last);
  318. /* Find the first data track */
  319. track = get_entry_track(gd.toc->last);
  320. do {
  321. data = gd.toc->entry[track - 1];
  322. if (get_entry_q_ctrl(data))
  323. break; /* ie a real data track */
  324. track--;
  325. } while (track >= fentry);
  326. if ((track > 100) || (track < get_entry_track(gd.toc->first))) {
  327. pr_info("No data on the last session of the CD\n");
  328. gdrom_getsense(NULL);
  329. return -ENXIO;
  330. }
  331. ms_info->addr_format = CDROM_LBA;
  332. ms_info->addr.lba = get_entry_lba(data);
  333. ms_info->xa_flag = 1;
  334. return 0;
  335. }
  336. static int gdrom_open(struct cdrom_device_info *cd_info, int purpose)
  337. {
  338. /* spin up the disk */
  339. return gdrom_preparedisk_cmd();
  340. }
  341. /* this function is required even if empty */
  342. static void gdrom_release(struct cdrom_device_info *cd_info)
  343. {
  344. }
  345. static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore)
  346. {
  347. /* read the sense key */
  348. char sense = ctrl_inb(GDROM_ERROR_REG);
  349. sense &= 0xF0;
  350. if (sense == 0)
  351. return CDS_DISC_OK;
  352. if (sense == 0x20)
  353. return CDS_DRIVE_NOT_READY;
  354. /* default */
  355. return CDS_NO_INFO;
  356. }
  357. static int gdrom_mediachanged(struct cdrom_device_info *cd_info, int ignore)
  358. {
  359. /* check the sense key */
  360. return (ctrl_inb(GDROM_ERROR_REG) & 0xF0) == 0x60;
  361. }
  362. /* reset the G1 bus */
  363. static int gdrom_hardreset(struct cdrom_device_info *cd_info)
  364. {
  365. int count;
  366. ctrl_outl(0x1fffff, GDROM_RESET_REG);
  367. for (count = 0xa0000000; count < 0xa0200000; count += 4)
  368. ctrl_inl(count);
  369. return 0;
  370. }
  371. /* keep the function looking like the universal
  372. * CD Rom specification - returning int */
  373. static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
  374. struct packet_command *command)
  375. {
  376. gdrom_spicommand(&command->cmd, command->buflen);
  377. return 0;
  378. }
  379. /* Get Sense SPI command
  380. * From Marcus Comstedt
  381. * cmd = 0x13
  382. * cmd + 4 = length of returned buffer
  383. * Returns 5 16 bit words
  384. */
  385. static int gdrom_getsense(short *bufstring)
  386. {
  387. struct packet_command *sense_command;
  388. short sense[5];
  389. int sense_key;
  390. int err = -EIO;
  391. sense_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
  392. if (!sense_command)
  393. return -ENOMEM;
  394. sense_command->cmd[0] = 0x13;
  395. sense_command->cmd[4] = 10;
  396. sense_command->buflen = 10;
  397. /* even if something is pending try to get
  398. * the sense key if possible */
  399. if (gd.pending && !gdrom_wait_clrbusy()) {
  400. err = -EBUSY;
  401. goto cleanup_sense_final;
  402. }
  403. gd.pending = 1;
  404. gdrom_packetcommand(gd.cd_info, sense_command);
  405. wait_event_interruptible_timeout(command_queue, gd.pending == 0,
  406. GDROM_DEFAULT_TIMEOUT);
  407. if (gd.pending)
  408. goto cleanup_sense;
  409. insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);
  410. if (sense[1] & 40) {
  411. pr_info("Drive not ready - command aborted\n");
  412. goto cleanup_sense;
  413. }
  414. sense_key = sense[1] & 0x0F;
  415. if (sense_key < ARRAY_SIZE(sense_texts))
  416. pr_info("%s\n", sense_texts[sense_key].text);
  417. else
  418. pr_err("Unknown sense key: %d\n", sense_key);
  419. if (bufstring) /* return addional sense data */
  420. memcpy(bufstring, &sense[4], 2);
  421. if (sense_key < 2)
  422. err = 0;
  423. cleanup_sense:
  424. gd.pending = 0;
  425. cleanup_sense_final:
  426. kfree(sense_command);
  427. return err;
  428. }
  429. static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
  430. void *arg)
  431. {
  432. return -EINVAL;
  433. }
  434. static struct cdrom_device_ops gdrom_ops = {
  435. .open = gdrom_open,
  436. .release = gdrom_release,
  437. .drive_status = gdrom_drivestatus,
  438. .media_changed = gdrom_mediachanged,
  439. .get_last_session = gdrom_get_last_session,
  440. .reset = gdrom_hardreset,
  441. .audio_ioctl = gdrom_audio_ioctl,
  442. .capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
  443. CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,
  444. .n_minors = 1,
  445. };
  446. static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
  447. {
  448. return cdrom_open(gd.cd_info, bdev, mode);
  449. }
  450. static int gdrom_bdops_release(struct gendisk *disk, fmode_t mode)
  451. {
  452. cdrom_release(gd.cd_info, mode);
  453. return 0;
  454. }
  455. static int gdrom_bdops_mediachanged(struct gendisk *disk)
  456. {
  457. return cdrom_media_changed(gd.cd_info);
  458. }
  459. static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode,
  460. unsigned cmd, unsigned long arg)
  461. {
  462. return cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
  463. }
  464. static const struct block_device_operations gdrom_bdops = {
  465. .owner = THIS_MODULE,
  466. .open = gdrom_bdops_open,
  467. .release = gdrom_bdops_release,
  468. .media_changed = gdrom_bdops_mediachanged,
  469. .locked_ioctl = gdrom_bdops_ioctl,
  470. };
  471. static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id)
  472. {
  473. gd.status = ctrl_inb(GDROM_STATUSCOMMAND_REG);
  474. if (gd.pending != 1)
  475. return IRQ_HANDLED;
  476. gd.pending = 0;
  477. wake_up_interruptible(&command_queue);
  478. return IRQ_HANDLED;
  479. }
  480. static irqreturn_t gdrom_dma_interrupt(int irq, void *dev_id)
  481. {
  482. gd.status = ctrl_inb(GDROM_STATUSCOMMAND_REG);
  483. if (gd.transfer != 1)
  484. return IRQ_HANDLED;
  485. gd.transfer = 0;
  486. wake_up_interruptible(&request_queue);
  487. return IRQ_HANDLED;
  488. }
  489. static int __devinit gdrom_set_interrupt_handlers(void)
  490. {
  491. int err;
  492. err = request_irq(HW_EVENT_GDROM_CMD, gdrom_command_interrupt,
  493. IRQF_DISABLED, "gdrom_command", &gd);
  494. if (err)
  495. return err;
  496. err = request_irq(HW_EVENT_GDROM_DMA, gdrom_dma_interrupt,
  497. IRQF_DISABLED, "gdrom_dma", &gd);
  498. if (err)
  499. free_irq(HW_EVENT_GDROM_CMD, &gd);
  500. return err;
  501. }
  502. /* Implement DMA read using SPI command
  503. * 0 -> 0x30
  504. * 1 -> mode
  505. * 2 -> block >> 16
  506. * 3 -> block >> 8
  507. * 4 -> block
  508. * 8 -> sectors >> 16
  509. * 9 -> sectors >> 8
  510. * 10 -> sectors
  511. */
  512. static void gdrom_readdisk_dma(struct work_struct *work)
  513. {
  514. int err, block, block_cnt;
  515. struct packet_command *read_command;
  516. struct list_head *elem, *next;
  517. struct request *req;
  518. unsigned long timeout;
  519. if (list_empty(&gdrom_deferred))
  520. return;
  521. read_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
  522. if (!read_command)
  523. return; /* get more memory later? */
  524. read_command->cmd[0] = 0x30;
  525. read_command->cmd[1] = 0x20;
  526. spin_lock(&gdrom_lock);
  527. list_for_each_safe(elem, next, &gdrom_deferred) {
  528. req = list_entry(elem, struct request, queuelist);
  529. spin_unlock(&gdrom_lock);
  530. block = blk_rq_pos(req)/GD_TO_BLK + GD_SESSION_OFFSET;
  531. block_cnt = blk_rq_sectors(req)/GD_TO_BLK;
  532. ctrl_outl(virt_to_phys(req->buffer), GDROM_DMA_STARTADDR_REG);
  533. ctrl_outl(block_cnt * GDROM_HARD_SECTOR, GDROM_DMA_LENGTH_REG);
  534. ctrl_outl(1, GDROM_DMA_DIRECTION_REG);
  535. ctrl_outl(1, GDROM_DMA_ENABLE_REG);
  536. read_command->cmd[2] = (block >> 16) & 0xFF;
  537. read_command->cmd[3] = (block >> 8) & 0xFF;
  538. read_command->cmd[4] = block & 0xFF;
  539. read_command->cmd[8] = (block_cnt >> 16) & 0xFF;
  540. read_command->cmd[9] = (block_cnt >> 8) & 0xFF;
  541. read_command->cmd[10] = block_cnt & 0xFF;
  542. /* set for DMA */
  543. ctrl_outb(1, GDROM_ERROR_REG);
  544. /* other registers */
  545. ctrl_outb(0, GDROM_SECNUM_REG);
  546. ctrl_outb(0, GDROM_BCL_REG);
  547. ctrl_outb(0, GDROM_BCH_REG);
  548. ctrl_outb(0, GDROM_DSEL_REG);
  549. ctrl_outb(0, GDROM_INTSEC_REG);
  550. /* Wait for registers to reset after any previous activity */
  551. timeout = jiffies + HZ / 2;
  552. while (gdrom_is_busy() && time_before(jiffies, timeout))
  553. cpu_relax();
  554. ctrl_outb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
  555. timeout = jiffies + HZ / 2;
  556. /* Wait for packet command to finish */
  557. while (gdrom_is_busy() && time_before(jiffies, timeout))
  558. cpu_relax();
  559. gd.pending = 1;
  560. gd.transfer = 1;
  561. outsw(GDROM_DATA_REG, &read_command->cmd, 6);
  562. timeout = jiffies + HZ / 2;
  563. /* Wait for any pending DMA to finish */
  564. while (ctrl_inb(GDROM_DMA_STATUS_REG) &&
  565. time_before(jiffies, timeout))
  566. cpu_relax();
  567. /* start transfer */
  568. ctrl_outb(1, GDROM_DMA_STATUS_REG);
  569. wait_event_interruptible_timeout(request_queue,
  570. gd.transfer == 0, GDROM_DEFAULT_TIMEOUT);
  571. err = gd.transfer ? -EIO : 0;
  572. gd.transfer = 0;
  573. gd.pending = 0;
  574. /* now seek to take the request spinlock
  575. * before handling ending the request */
  576. spin_lock(&gdrom_lock);
  577. list_del_init(&req->queuelist);
  578. __blk_end_request_all(req, err);
  579. }
  580. spin_unlock(&gdrom_lock);
  581. kfree(read_command);
  582. }
  583. static void gdrom_request(struct request_queue *rq)
  584. {
  585. struct request *req;
  586. while ((req = blk_fetch_request(rq)) != NULL) {
  587. if (req->cmd_type != REQ_TYPE_FS) {
  588. printk(KERN_DEBUG "gdrom: Non-fs request ignored\n");
  589. __blk_end_request_all(req, -EIO);
  590. continue;
  591. }
  592. if (rq_data_dir(req) != READ) {
  593. pr_notice("Read only device - write request ignored\n");
  594. __blk_end_request_all(req, -EIO);
  595. continue;
  596. }
  597. /*
  598. * Add to list of deferred work and then schedule
  599. * workqueue.
  600. */
  601. list_add_tail(&req->queuelist, &gdrom_deferred);
  602. schedule_work(&work);
  603. }
  604. }
  605. /* Print string identifying GD ROM device */
  606. static int __devinit gdrom_outputversion(void)
  607. {
  608. struct gdrom_id *id;
  609. char *model_name, *manuf_name, *firmw_ver;
  610. int err = -ENOMEM;
  611. /* query device ID */
  612. id = kzalloc(sizeof(struct gdrom_id), GFP_KERNEL);
  613. if (!id)
  614. return err;
  615. gdrom_identifydevice(id);
  616. model_name = kstrndup(id->modname, 16, GFP_KERNEL);
  617. if (!model_name)
  618. goto free_id;
  619. manuf_name = kstrndup(id->mname, 16, GFP_KERNEL);
  620. if (!manuf_name)
  621. goto free_model_name;
  622. firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL);
  623. if (!firmw_ver)
  624. goto free_manuf_name;
  625. pr_info("%s from %s with firmware %s\n",
  626. model_name, manuf_name, firmw_ver);
  627. err = 0;
  628. kfree(firmw_ver);
  629. free_manuf_name:
  630. kfree(manuf_name);
  631. free_model_name:
  632. kfree(model_name);
  633. free_id:
  634. kfree(id);
  635. return err;
  636. }
  637. /* set the default mode for DMA transfer */
  638. static int __devinit gdrom_init_dma_mode(void)
  639. {
  640. ctrl_outb(0x13, GDROM_ERROR_REG);
  641. ctrl_outb(0x22, GDROM_INTSEC_REG);
  642. if (!gdrom_wait_clrbusy())
  643. return -EBUSY;
  644. ctrl_outb(0xEF, GDROM_STATUSCOMMAND_REG);
  645. if (!gdrom_wait_busy_sleeps())
  646. return -EBUSY;
  647. /* Memory protection setting for GDROM DMA
  648. * Bits 31 - 16 security: 0x8843
  649. * Bits 15 and 7 reserved (0)
  650. * Bits 14 - 8 start of transfer range in 1 MB blocks OR'ed with 0x80
  651. * Bits 6 - 0 end of transfer range in 1 MB blocks OR'ed with 0x80
  652. * (0x40 | 0x80) = start range at 0x0C000000
  653. * (0x7F | 0x80) = end range at 0x0FFFFFFF */
  654. ctrl_outl(0x8843407F, GDROM_DMA_ACCESS_CTRL_REG);
  655. ctrl_outl(9, GDROM_DMA_WAIT_REG); /* DMA word setting */
  656. return 0;
  657. }
  658. static void __devinit probe_gdrom_setupcd(void)
  659. {
  660. gd.cd_info->ops = &gdrom_ops;
  661. gd.cd_info->capacity = 1;
  662. strcpy(gd.cd_info->name, GDROM_DEV_NAME);
  663. gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
  664. CDC_SELECT_DISC;
  665. }
  666. static void __devinit probe_gdrom_setupdisk(void)
  667. {
  668. gd.disk->major = gdrom_major;
  669. gd.disk->first_minor = 1;
  670. gd.disk->minors = 1;
  671. strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
  672. }
  673. static int __devinit probe_gdrom_setupqueue(void)
  674. {
  675. blk_queue_logical_block_size(gd.gdrom_rq, GDROM_HARD_SECTOR);
  676. /* using DMA so memory will need to be contiguous */
  677. blk_queue_max_segments(gd.gdrom_rq, 1);
  678. /* set a large max size to get most from DMA */
  679. blk_queue_max_segment_size(gd.gdrom_rq, 0x40000);
  680. gd.disk->queue = gd.gdrom_rq;
  681. return gdrom_init_dma_mode();
  682. }
  683. /*
  684. * register this as a block device and as compliant with the
  685. * universal CD Rom driver interface
  686. */
  687. static int __devinit probe_gdrom(struct platform_device *devptr)
  688. {
  689. int err;
  690. /* Start the device */
  691. if (gdrom_execute_diagnostic() != 1) {
  692. pr_warning("ATA Probe for GDROM failed\n");
  693. return -ENODEV;
  694. }
  695. /* Print out firmware ID */
  696. if (gdrom_outputversion())
  697. return -ENOMEM;
  698. /* Register GDROM */
  699. gdrom_major = register_blkdev(0, GDROM_DEV_NAME);
  700. if (gdrom_major <= 0)
  701. return gdrom_major;
  702. pr_info("Registered with major number %d\n",
  703. gdrom_major);
  704. /* Specify basic properties of drive */
  705. gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL);
  706. if (!gd.cd_info) {
  707. err = -ENOMEM;
  708. goto probe_fail_no_mem;
  709. }
  710. probe_gdrom_setupcd();
  711. gd.disk = alloc_disk(1);
  712. if (!gd.disk) {
  713. err = -ENODEV;
  714. goto probe_fail_no_disk;
  715. }
  716. probe_gdrom_setupdisk();
  717. if (register_cdrom(gd.cd_info)) {
  718. err = -ENODEV;
  719. goto probe_fail_cdrom_register;
  720. }
  721. gd.disk->fops = &gdrom_bdops;
  722. /* latch on to the interrupt */
  723. err = gdrom_set_interrupt_handlers();
  724. if (err)
  725. goto probe_fail_cmdirq_register;
  726. gd.gdrom_rq = blk_init_queue(gdrom_request, &gdrom_lock);
  727. if (!gd.gdrom_rq)
  728. goto probe_fail_requestq;
  729. err = probe_gdrom_setupqueue();
  730. if (err)
  731. goto probe_fail_toc;
  732. gd.toc = kzalloc(sizeof(struct gdromtoc), GFP_KERNEL);
  733. if (!gd.toc)
  734. goto probe_fail_toc;
  735. add_disk(gd.disk);
  736. return 0;
  737. probe_fail_toc:
  738. blk_cleanup_queue(gd.gdrom_rq);
  739. probe_fail_requestq:
  740. free_irq(HW_EVENT_GDROM_DMA, &gd);
  741. free_irq(HW_EVENT_GDROM_CMD, &gd);
  742. probe_fail_cmdirq_register:
  743. probe_fail_cdrom_register:
  744. del_gendisk(gd.disk);
  745. probe_fail_no_disk:
  746. kfree(gd.cd_info);
  747. unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
  748. gdrom_major = 0;
  749. probe_fail_no_mem:
  750. pr_warning("Probe failed - error is 0x%X\n", err);
  751. return err;
  752. }
  753. static int __devexit remove_gdrom(struct platform_device *devptr)
  754. {
  755. flush_scheduled_work();
  756. blk_cleanup_queue(gd.gdrom_rq);
  757. free_irq(HW_EVENT_GDROM_CMD, &gd);
  758. free_irq(HW_EVENT_GDROM_DMA, &gd);
  759. del_gendisk(gd.disk);
  760. if (gdrom_major)
  761. unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
  762. unregister_cdrom(gd.cd_info);
  763. return 0;
  764. }
  765. static struct platform_driver gdrom_driver = {
  766. .probe = probe_gdrom,
  767. .remove = __devexit_p(remove_gdrom),
  768. .driver = {
  769. .name = GDROM_DEV_NAME,
  770. },
  771. };
  772. static int __init init_gdrom(void)
  773. {
  774. int rc;
  775. gd.toc = NULL;
  776. rc = platform_driver_register(&gdrom_driver);
  777. if (rc)
  778. return rc;
  779. pd = platform_device_register_simple(GDROM_DEV_NAME, -1, NULL, 0);
  780. if (IS_ERR(pd)) {
  781. platform_driver_unregister(&gdrom_driver);
  782. return PTR_ERR(pd);
  783. }
  784. return 0;
  785. }
  786. static void __exit exit_gdrom(void)
  787. {
  788. platform_device_unregister(pd);
  789. platform_driver_unregister(&gdrom_driver);
  790. kfree(gd.toc);
  791. }
  792. module_init(init_gdrom);
  793. module_exit(exit_gdrom);
  794. MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
  795. MODULE_DESCRIPTION("SEGA Dreamcast GD-ROM Driver");
  796. MODULE_LICENSE("GPL");