gdrom.c 23 KB

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