gdrom.c 23 KB

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