ide-v10.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /* $Id: ide.c,v 1.4 2004/10/12 07:55:48 starvik Exp $
  2. *
  3. * Etrax specific IDE functions, like init and PIO-mode setting etc.
  4. * Almost the entire ide.c is used for the rest of the Etrax ATA driver.
  5. * Copyright (c) 2000-2004 Axis Communications AB
  6. *
  7. * Authors: Bjorn Wesen (initial version)
  8. * Mikael Starvik (pio setup stuff, Linux 2.6 port)
  9. */
  10. /* Regarding DMA:
  11. *
  12. * There are two forms of DMA - "DMA handshaking" between the interface and the drive,
  13. * and DMA between the memory and the interface. We can ALWAYS use the latter, since it's
  14. * something built-in in the Etrax. However only some drives support the DMA-mode handshaking
  15. * on the ATA-bus. The normal PC driver and Triton interface disables memory-if DMA when the
  16. * device can't do DMA handshaking for some stupid reason. We don't need to do that.
  17. */
  18. #undef REALLY_SLOW_IO /* most systems can safely undef this */
  19. #include <linux/config.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/timer.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/hdreg.h>
  28. #include <linux/ide.h>
  29. #include <linux/init.h>
  30. #include <linux/scatterlist.h>
  31. #include <asm/io.h>
  32. #include <asm/arch/svinto.h>
  33. #include <asm/dma.h>
  34. /* number of Etrax DMA descriptors */
  35. #define MAX_DMA_DESCRS 64
  36. /* number of times to retry busy-flags when reading/writing IDE-registers
  37. * this can't be too high because a hung harddisk might cause the watchdog
  38. * to trigger (sometimes INB and OUTB are called with irq's disabled)
  39. */
  40. #define IDE_REGISTER_TIMEOUT 300
  41. static int e100_read_command = 0;
  42. #define LOWDB(x)
  43. #define D(x)
  44. static int e100_ide_build_dmatable (ide_drive_t *drive);
  45. static ide_startstop_t etrax_dma_intr (ide_drive_t *drive);
  46. void
  47. etrax100_ide_outw(unsigned short data, unsigned long reg) {
  48. int timeleft;
  49. LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg));
  50. /* note the lack of handling any timeouts. we stop waiting, but we don't
  51. * really notify anybody.
  52. */
  53. timeleft = IDE_REGISTER_TIMEOUT;
  54. /* wait for busy flag */
  55. while(timeleft && (*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)))
  56. timeleft--;
  57. /*
  58. * Fall through at a timeout, so the ongoing command will be
  59. * aborted by the write below, which is expected to be a dummy
  60. * command to the command register. This happens when a faulty
  61. * drive times out on a command. See comment on timeout in
  62. * INB.
  63. */
  64. if(!timeleft)
  65. printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data);
  66. *R_ATA_CTRL_DATA = reg | data; /* write data to the drive's register */
  67. timeleft = IDE_REGISTER_TIMEOUT;
  68. /* wait for transmitter ready */
  69. while(timeleft && !(*R_ATA_STATUS_DATA &
  70. IO_MASK(R_ATA_STATUS_DATA, tr_rdy)))
  71. timeleft--;
  72. }
  73. void
  74. etrax100_ide_outb(unsigned char data, unsigned long reg)
  75. {
  76. etrax100_ide_outw(data, reg);
  77. }
  78. void
  79. etrax100_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port)
  80. {
  81. etrax100_ide_outw(addr, port);
  82. }
  83. unsigned short
  84. etrax100_ide_inw(unsigned long reg) {
  85. int status;
  86. int timeleft;
  87. timeleft = IDE_REGISTER_TIMEOUT;
  88. /* wait for busy flag */
  89. while(timeleft && (*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)))
  90. timeleft--;
  91. if(!timeleft) {
  92. /*
  93. * If we're asked to read the status register, like for
  94. * example when a command does not complete for an
  95. * extended time, but the ATA interface is stuck in a
  96. * busy state at the *ETRAX* ATA interface level (as has
  97. * happened repeatedly with at least one bad disk), then
  98. * the best thing to do is to pretend that we read
  99. * "busy" in the status register, so the IDE driver will
  100. * time-out, abort the ongoing command and perform a
  101. * reset sequence. Note that the subsequent OUT_BYTE
  102. * call will also timeout on busy, but as long as the
  103. * write is still performed, everything will be fine.
  104. */
  105. if ((reg & IO_MASK (R_ATA_CTRL_DATA, addr))
  106. == IO_FIELD (R_ATA_CTRL_DATA, addr, IDE_STATUS_OFFSET))
  107. return BUSY_STAT;
  108. else
  109. /* For other rare cases we assume 0 is good enough. */
  110. return 0;
  111. }
  112. *R_ATA_CTRL_DATA = reg | IO_STATE(R_ATA_CTRL_DATA, rw, read); /* read data */
  113. timeleft = IDE_REGISTER_TIMEOUT;
  114. /* wait for available */
  115. while(timeleft && !((status = *R_ATA_STATUS_DATA) &
  116. IO_MASK(R_ATA_STATUS_DATA, dav)))
  117. timeleft--;
  118. if(!timeleft)
  119. return 0;
  120. LOWDB(printk("inb: 0x%x from reg 0x%x\n", status & 0xff, reg));
  121. return (unsigned short)status;
  122. }
  123. unsigned char
  124. etrax100_ide_inb(unsigned long reg)
  125. {
  126. return (unsigned char)etrax100_ide_inw(reg);
  127. }
  128. /* PIO timing (in R_ATA_CONFIG)
  129. *
  130. * _____________________________
  131. * ADDRESS : ________/
  132. *
  133. * _______________
  134. * DIOR : ____________/ \__________
  135. *
  136. * _______________
  137. * DATA : XXXXXXXXXXXXXXXX_______________XXXXXXXX
  138. *
  139. *
  140. * DIOR is unbuffered while address and data is buffered.
  141. * This creates two problems:
  142. * 1. The DIOR pulse is to early (because it is unbuffered)
  143. * 2. The rise time of DIOR is long
  144. *
  145. * There are at least three different plausible solutions
  146. * 1. Use a pad capable of larger currents in Etrax
  147. * 2. Use an external buffer
  148. * 3. Make the strobe pulse longer
  149. *
  150. * Some of the strobe timings below are modified to compensate
  151. * for this. This implies a slight performance decrease.
  152. *
  153. * THIS SHOULD NEVER BE CHANGED!
  154. *
  155. * TODO: Is this true for the latest LX boards still ?
  156. */
  157. #define ATA_DMA2_STROBE 4
  158. #define ATA_DMA2_HOLD 0
  159. #define ATA_DMA1_STROBE 4
  160. #define ATA_DMA1_HOLD 1
  161. #define ATA_DMA0_STROBE 12
  162. #define ATA_DMA0_HOLD 9
  163. #define ATA_PIO4_SETUP 1
  164. #define ATA_PIO4_STROBE 5
  165. #define ATA_PIO4_HOLD 0
  166. #define ATA_PIO3_SETUP 1
  167. #define ATA_PIO3_STROBE 5
  168. #define ATA_PIO3_HOLD 1
  169. #define ATA_PIO2_SETUP 1
  170. #define ATA_PIO2_STROBE 6
  171. #define ATA_PIO2_HOLD 2
  172. #define ATA_PIO1_SETUP 2
  173. #define ATA_PIO1_STROBE 11
  174. #define ATA_PIO1_HOLD 4
  175. #define ATA_PIO0_SETUP 4
  176. #define ATA_PIO0_STROBE 19
  177. #define ATA_PIO0_HOLD 4
  178. static int e100_dma_check (ide_drive_t *drive);
  179. static void e100_dma_start(ide_drive_t *drive);
  180. static int e100_dma_end (ide_drive_t *drive);
  181. static void e100_ide_input_data (ide_drive_t *drive, void *, unsigned int);
  182. static void e100_ide_output_data (ide_drive_t *drive, void *, unsigned int);
  183. static void e100_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);
  184. static void e100_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);
  185. static int e100_dma_off (ide_drive_t *drive);
  186. /*
  187. * good_dma_drives() lists the model names (from "hdparm -i")
  188. * of drives which do not support mword2 DMA but which are
  189. * known to work fine with this interface under Linux.
  190. */
  191. const char *good_dma_drives[] = {"Micropolis 2112A",
  192. "CONNER CTMA 4000",
  193. "CONNER CTT8000-A",
  194. NULL};
  195. static void tune_e100_ide(ide_drive_t *drive, byte pio)
  196. {
  197. pio = 4;
  198. /* pio = ide_get_best_pio_mode(drive, pio, 4, NULL); */
  199. /* set pio mode! */
  200. switch(pio) {
  201. case 0:
  202. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  203. IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) |
  204. IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) |
  205. IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO0_SETUP ) |
  206. IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO0_STROBE ) |
  207. IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO0_HOLD ) );
  208. break;
  209. case 1:
  210. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  211. IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) |
  212. IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) |
  213. IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO1_SETUP ) |
  214. IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO1_STROBE ) |
  215. IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO1_HOLD ) );
  216. break;
  217. case 2:
  218. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  219. IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) |
  220. IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) |
  221. IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO2_SETUP ) |
  222. IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO2_STROBE ) |
  223. IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO2_HOLD ) );
  224. break;
  225. case 3:
  226. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  227. IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) |
  228. IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) |
  229. IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO3_SETUP ) |
  230. IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO3_STROBE ) |
  231. IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO3_HOLD ) );
  232. break;
  233. case 4:
  234. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  235. IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) |
  236. IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) |
  237. IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO4_SETUP ) |
  238. IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO4_STROBE ) |
  239. IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO4_HOLD ) );
  240. break;
  241. }
  242. }
  243. static int e100_dma_setup(ide_drive_t *drive)
  244. {
  245. struct request *rq = drive->hwif->hwgroup->rq;
  246. if (rq_data_dir(rq)) {
  247. e100_read_command = 0;
  248. RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
  249. WAIT_DMA(ATA_TX_DMA_NBR);
  250. } else {
  251. e100_read_command = 1;
  252. RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
  253. WAIT_DMA(ATA_RX_DMA_NBR);
  254. }
  255. /* set up the Etrax DMA descriptors */
  256. if (e100_ide_build_dmatable(drive)) {
  257. ide_map_sg(drive, rq);
  258. return 1;
  259. }
  260. return 0;
  261. }
  262. static void e100_dma_exec_cmd(ide_drive_t *drive, u8 command)
  263. {
  264. /* set the irq handler which will finish the request when DMA is done */
  265. ide_set_handler(drive, &etrax_dma_intr, WAIT_CMD, NULL);
  266. /* issue cmd to drive */
  267. etrax100_ide_outb(command, IDE_COMMAND_REG);
  268. }
  269. void __init
  270. init_e100_ide (void)
  271. {
  272. volatile unsigned int dummy;
  273. int h;
  274. printk("ide: ETRAX 100LX built-in ATA DMA controller\n");
  275. /* first fill in some stuff in the ide_hwifs fields */
  276. for(h = 0; h < MAX_HWIFS; h++) {
  277. ide_hwif_t *hwif = &ide_hwifs[h];
  278. hwif->mmio = 2;
  279. hwif->chipset = ide_etrax100;
  280. hwif->tuneproc = &tune_e100_ide;
  281. hwif->ata_input_data = &e100_ide_input_data;
  282. hwif->ata_output_data = &e100_ide_output_data;
  283. hwif->atapi_input_bytes = &e100_atapi_input_bytes;
  284. hwif->atapi_output_bytes = &e100_atapi_output_bytes;
  285. hwif->ide_dma_check = &e100_dma_check;
  286. hwif->ide_dma_end = &e100_dma_end;
  287. hwif->dma_setup = &e100_dma_setup;
  288. hwif->dma_exec_cmd = &e100_dma_exec_cmd;
  289. hwif->dma_start = &e100_dma_start;
  290. hwif->OUTB = &etrax100_ide_outb;
  291. hwif->OUTW = &etrax100_ide_outw;
  292. hwif->OUTBSYNC = &etrax100_ide_outbsync;
  293. hwif->INB = &etrax100_ide_inb;
  294. hwif->INW = &etrax100_ide_inw;
  295. hwif->ide_dma_off_quietly = &e100_dma_off;
  296. }
  297. /* actually reset and configure the etrax100 ide/ata interface */
  298. *R_ATA_CTRL_DATA = 0;
  299. *R_ATA_TRANSFER_CNT = 0;
  300. *R_ATA_CONFIG = 0;
  301. genconfig_shadow = (genconfig_shadow &
  302. ~IO_MASK(R_GEN_CONFIG, dma2) &
  303. ~IO_MASK(R_GEN_CONFIG, dma3) &
  304. ~IO_MASK(R_GEN_CONFIG, ata)) |
  305. ( IO_STATE( R_GEN_CONFIG, dma3, ata ) |
  306. IO_STATE( R_GEN_CONFIG, dma2, ata ) |
  307. IO_STATE( R_GEN_CONFIG, ata, select ) );
  308. *R_GEN_CONFIG = genconfig_shadow;
  309. /* pull the chosen /reset-line low */
  310. #ifdef CONFIG_ETRAX_IDE_G27_RESET
  311. REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, 0);
  312. #endif
  313. #ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET
  314. REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, 0);
  315. #endif
  316. #ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET
  317. REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, 0);
  318. #endif
  319. #ifdef CONFIG_ETRAX_IDE_PB7_RESET
  320. port_pb_dir_shadow = port_pb_dir_shadow |
  321. IO_STATE(R_PORT_PB_DIR, dir7, output);
  322. *R_PORT_PB_DIR = port_pb_dir_shadow;
  323. REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, 1);
  324. #endif
  325. /* wait some */
  326. udelay(25);
  327. /* de-assert bus-reset */
  328. #ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET
  329. REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, 1);
  330. #endif
  331. #ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET
  332. REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, 1);
  333. #endif
  334. #ifdef CONFIG_ETRAX_IDE_G27_RESET
  335. REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, 1);
  336. #endif
  337. /* make a dummy read to set the ata controller in a proper state */
  338. dummy = *R_ATA_STATUS_DATA;
  339. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  340. IO_FIELD( R_ATA_CONFIG, dma_strobe, ATA_DMA2_STROBE ) |
  341. IO_FIELD( R_ATA_CONFIG, dma_hold, ATA_DMA2_HOLD ) |
  342. IO_FIELD( R_ATA_CONFIG, pio_setup, ATA_PIO4_SETUP ) |
  343. IO_FIELD( R_ATA_CONFIG, pio_strobe, ATA_PIO4_STROBE ) |
  344. IO_FIELD( R_ATA_CONFIG, pio_hold, ATA_PIO4_HOLD ) );
  345. *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw, read) |
  346. IO_FIELD( R_ATA_CTRL_DATA, addr, 1 ) );
  347. while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/
  348. *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) |
  349. IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) |
  350. IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) |
  351. IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) );
  352. printk("ide: waiting %d seconds for drives to regain consciousness\n",
  353. CONFIG_ETRAX_IDE_DELAY);
  354. h = jiffies + (CONFIG_ETRAX_IDE_DELAY * HZ);
  355. while(time_before(jiffies, h)) /* nothing */ ;
  356. /* reset the dma channels we will use */
  357. RESET_DMA(ATA_TX_DMA_NBR);
  358. RESET_DMA(ATA_RX_DMA_NBR);
  359. WAIT_DMA(ATA_TX_DMA_NBR);
  360. WAIT_DMA(ATA_RX_DMA_NBR);
  361. }
  362. static int e100_dma_off (ide_drive_t *drive)
  363. {
  364. return 0;
  365. }
  366. static etrax_dma_descr mydescr;
  367. /*
  368. * The following routines are mainly used by the ATAPI drivers.
  369. *
  370. * These routines will round up any request for an odd number of bytes,
  371. * so if an odd bytecount is specified, be sure that there's at least one
  372. * extra byte allocated for the buffer.
  373. */
  374. static void
  375. e100_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
  376. {
  377. unsigned long data_reg = IDE_DATA_REG;
  378. D(printk("atapi_input_bytes, dreg 0x%x, buffer 0x%x, count %d\n",
  379. data_reg, buffer, bytecount));
  380. if(bytecount & 1) {
  381. printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount);
  382. bytecount++; /* to round off */
  383. }
  384. /* make sure the DMA channel is available */
  385. RESET_DMA(ATA_RX_DMA_NBR);
  386. WAIT_DMA(ATA_RX_DMA_NBR);
  387. /* setup DMA descriptor */
  388. mydescr.sw_len = bytecount;
  389. mydescr.ctrl = d_eol;
  390. mydescr.buf = virt_to_phys(buffer);
  391. /* start the dma channel */
  392. *R_DMA_CH3_FIRST = virt_to_phys(&mydescr);
  393. *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start);
  394. /* initiate a multi word dma read using PIO handshaking */
  395. *R_ATA_TRANSFER_CNT = IO_FIELD(R_ATA_TRANSFER_CNT, count, bytecount >> 1);
  396. *R_ATA_CTRL_DATA = data_reg |
  397. IO_STATE(R_ATA_CTRL_DATA, rw, read) |
  398. IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) |
  399. IO_STATE(R_ATA_CTRL_DATA, handsh, pio) |
  400. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  401. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  402. /* wait for completion */
  403. LED_DISK_READ(1);
  404. WAIT_DMA(ATA_RX_DMA_NBR);
  405. LED_DISK_READ(0);
  406. #if 0
  407. /* old polled transfer code
  408. * this should be moved into a new function that can do polled
  409. * transfers if DMA is not available
  410. */
  411. /* initiate a multi word read */
  412. *R_ATA_TRANSFER_CNT = wcount << 1;
  413. *R_ATA_CTRL_DATA = data_reg |
  414. IO_STATE(R_ATA_CTRL_DATA, rw, read) |
  415. IO_STATE(R_ATA_CTRL_DATA, src_dst, register) |
  416. IO_STATE(R_ATA_CTRL_DATA, handsh, pio) |
  417. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  418. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  419. /* svinto has a latency until the busy bit actually is set */
  420. nop(); nop();
  421. nop(); nop();
  422. nop(); nop();
  423. nop(); nop();
  424. nop(); nop();
  425. /* unit should be busy during multi transfer */
  426. while((status = *R_ATA_STATUS_DATA) & IO_MASK(R_ATA_STATUS_DATA, busy)) {
  427. while(!(status & IO_MASK(R_ATA_STATUS_DATA, dav)))
  428. status = *R_ATA_STATUS_DATA;
  429. *ptr++ = (unsigned short)(status & 0xffff);
  430. }
  431. #endif
  432. }
  433. static void
  434. e100_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
  435. {
  436. unsigned long data_reg = IDE_DATA_REG;
  437. D(printk("atapi_output_bytes, dreg 0x%x, buffer 0x%x, count %d\n",
  438. data_reg, buffer, bytecount));
  439. if(bytecount & 1) {
  440. printk("odd bytecount %d in atapi_out_bytes!\n", bytecount);
  441. bytecount++;
  442. }
  443. /* make sure the DMA channel is available */
  444. RESET_DMA(ATA_TX_DMA_NBR);
  445. WAIT_DMA(ATA_TX_DMA_NBR);
  446. /* setup DMA descriptor */
  447. mydescr.sw_len = bytecount;
  448. mydescr.ctrl = d_eol;
  449. mydescr.buf = virt_to_phys(buffer);
  450. /* start the dma channel */
  451. *R_DMA_CH2_FIRST = virt_to_phys(&mydescr);
  452. *R_DMA_CH2_CMD = IO_STATE(R_DMA_CH2_CMD, cmd, start);
  453. /* initiate a multi word dma write using PIO handshaking */
  454. *R_ATA_TRANSFER_CNT = IO_FIELD(R_ATA_TRANSFER_CNT, count, bytecount >> 1);
  455. *R_ATA_CTRL_DATA = data_reg |
  456. IO_STATE(R_ATA_CTRL_DATA, rw, write) |
  457. IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) |
  458. IO_STATE(R_ATA_CTRL_DATA, handsh, pio) |
  459. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  460. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  461. /* wait for completion */
  462. LED_DISK_WRITE(1);
  463. WAIT_DMA(ATA_TX_DMA_NBR);
  464. LED_DISK_WRITE(0);
  465. #if 0
  466. /* old polled write code - see comment in input_bytes */
  467. /* wait for busy flag */
  468. while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy));
  469. /* initiate a multi word write */
  470. *R_ATA_TRANSFER_CNT = bytecount >> 1;
  471. ctrl = data_reg |
  472. IO_STATE(R_ATA_CTRL_DATA, rw, write) |
  473. IO_STATE(R_ATA_CTRL_DATA, src_dst, register) |
  474. IO_STATE(R_ATA_CTRL_DATA, handsh, pio) |
  475. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  476. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  477. LED_DISK_WRITE(1);
  478. /* Etrax will set busy = 1 until the multi pio transfer has finished
  479. * and tr_rdy = 1 after each successful word transfer.
  480. * When the last byte has been transferred Etrax will first set tr_tdy = 1
  481. * and then busy = 0 (not in the same cycle). If we read busy before it
  482. * has been set to 0 we will think that we should transfer more bytes
  483. * and then tr_rdy would be 0 forever. This is solved by checking busy
  484. * in the inner loop.
  485. */
  486. do {
  487. *R_ATA_CTRL_DATA = ctrl | *ptr++;
  488. while(!(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, tr_rdy)) &&
  489. (*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)));
  490. } while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy));
  491. LED_DISK_WRITE(0);
  492. #endif
  493. }
  494. /*
  495. * This is used for most PIO data transfers *from* the IDE interface
  496. */
  497. static void
  498. e100_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
  499. {
  500. e100_atapi_input_bytes(drive, buffer, wcount << 2);
  501. }
  502. /*
  503. * This is used for most PIO data transfers *to* the IDE interface
  504. */
  505. static void
  506. e100_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
  507. {
  508. e100_atapi_output_bytes(drive, buffer, wcount << 2);
  509. }
  510. /* we only have one DMA channel on the chip for ATA, so we can keep these statically */
  511. static etrax_dma_descr ata_descrs[MAX_DMA_DESCRS];
  512. static unsigned int ata_tot_size;
  513. /*
  514. * e100_ide_build_dmatable() prepares a dma request.
  515. * Returns 0 if all went okay, returns 1 otherwise.
  516. */
  517. static int e100_ide_build_dmatable (ide_drive_t *drive)
  518. {
  519. ide_hwif_t *hwif = HWIF(drive);
  520. struct scatterlist* sg;
  521. struct request *rq = HWGROUP(drive)->rq;
  522. unsigned long size, addr;
  523. unsigned int count = 0;
  524. int i = 0;
  525. sg = hwif->sg_table;
  526. ata_tot_size = 0;
  527. ide_map_sg(drive, rq);
  528. i = hwif->sg_nents;
  529. while(i) {
  530. /*
  531. * Determine addr and size of next buffer area. We assume that
  532. * individual virtual buffers are always composed linearly in
  533. * physical memory. For example, we assume that any 8kB buffer
  534. * is always composed of two adjacent physical 4kB pages rather
  535. * than two possibly non-adjacent physical 4kB pages.
  536. */
  537. /* group sequential buffers into one large buffer */
  538. addr = page_to_phys(sg->page) + sg->offset;
  539. size = sg_dma_len(sg);
  540. while (sg++, --i) {
  541. if ((addr + size) != page_to_phys(sg->page) + sg->offset)
  542. break;
  543. size += sg_dma_len(sg);
  544. }
  545. /* did we run out of descriptors? */
  546. if(count >= MAX_DMA_DESCRS) {
  547. printk("%s: too few DMA descriptors\n", drive->name);
  548. return 1;
  549. }
  550. /* however, this case is more difficult - R_ATA_TRANSFER_CNT cannot be more
  551. than 65536 words per transfer, so in that case we need to either
  552. 1) use a DMA interrupt to re-trigger R_ATA_TRANSFER_CNT and continue with
  553. the descriptors, or
  554. 2) simply do the request here, and get dma_intr to only ide_end_request on
  555. those blocks that were actually set-up for transfer.
  556. */
  557. if(ata_tot_size + size > 131072) {
  558. printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size);
  559. return 1;
  560. }
  561. /* If size > 65536 it has to be splitted into new descriptors. Since we don't handle
  562. size > 131072 only one split is necessary */
  563. if(size > 65536) {
  564. /* ok we want to do IO at addr, size bytes. set up a new descriptor entry */
  565. ata_descrs[count].sw_len = 0; /* 0 means 65536, this is a 16-bit field */
  566. ata_descrs[count].ctrl = 0;
  567. ata_descrs[count].buf = addr;
  568. ata_descrs[count].next = virt_to_phys(&ata_descrs[count + 1]);
  569. count++;
  570. ata_tot_size += 65536;
  571. /* size and addr should refere to not handled data */
  572. size -= 65536;
  573. addr += 65536;
  574. }
  575. /* ok we want to do IO at addr, size bytes. set up a new descriptor entry */
  576. if(size == 65536) {
  577. ata_descrs[count].sw_len = 0; /* 0 means 65536, this is a 16-bit field */
  578. } else {
  579. ata_descrs[count].sw_len = size;
  580. }
  581. ata_descrs[count].ctrl = 0;
  582. ata_descrs[count].buf = addr;
  583. ata_descrs[count].next = virt_to_phys(&ata_descrs[count + 1]);
  584. count++;
  585. ata_tot_size += size;
  586. }
  587. if (count) {
  588. /* set the end-of-list flag on the last descriptor */
  589. ata_descrs[count - 1].ctrl |= d_eol;
  590. /* return and say all is ok */
  591. return 0;
  592. }
  593. printk("%s: empty DMA table?\n", drive->name);
  594. return 1; /* let the PIO routines handle this weirdness */
  595. }
  596. static int config_drive_for_dma (ide_drive_t *drive)
  597. {
  598. const char **list;
  599. struct hd_driveid *id = drive->id;
  600. if (id && (id->capability & 1)) {
  601. /* Enable DMA on any drive that supports mword2 DMA */
  602. if ((id->field_valid & 2) && (id->dma_mword & 0x404) == 0x404) {
  603. drive->using_dma = 1;
  604. return 0; /* DMA enabled */
  605. }
  606. /* Consult the list of known "good" drives */
  607. list = good_dma_drives;
  608. while (*list) {
  609. if (!strcmp(*list++,id->model)) {
  610. drive->using_dma = 1;
  611. return 0; /* DMA enabled */
  612. }
  613. }
  614. }
  615. return 1; /* DMA not enabled */
  616. }
  617. /*
  618. * etrax_dma_intr() is the handler for disk read/write DMA interrupts
  619. */
  620. static ide_startstop_t etrax_dma_intr (ide_drive_t *drive)
  621. {
  622. LED_DISK_READ(0);
  623. LED_DISK_WRITE(0);
  624. return ide_dma_intr(drive);
  625. }
  626. /*
  627. * Functions below initiates/aborts DMA read/write operations on a drive.
  628. *
  629. * The caller is assumed to have selected the drive and programmed the drive's
  630. * sector address using CHS or LBA. All that remains is to prepare for DMA
  631. * and then issue the actual read/write DMA/PIO command to the drive.
  632. *
  633. * Returns 0 if all went well.
  634. * Returns 1 if DMA read/write could not be started, in which case
  635. * the caller should revert to PIO for the current request.
  636. */
  637. static int e100_dma_check(ide_drive_t *drive)
  638. {
  639. return config_drive_for_dma (drive);
  640. }
  641. static int e100_dma_end(ide_drive_t *drive)
  642. {
  643. /* TODO: check if something went wrong with the DMA */
  644. return 0;
  645. }
  646. static void e100_dma_start(ide_drive_t *drive)
  647. {
  648. if (e100_read_command) {
  649. /* begin DMA */
  650. /* need to do this before RX DMA due to a chip bug
  651. * it is enough to just flush the part of the cache that
  652. * corresponds to the buffers we start, but since HD transfers
  653. * usually are more than 8 kB, it is easier to optimize for the
  654. * normal case and just flush the entire cache. its the only
  655. * way to be sure! (OB movie quote)
  656. */
  657. flush_etrax_cache();
  658. *R_DMA_CH3_FIRST = virt_to_phys(ata_descrs);
  659. *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start);
  660. /* initiate a multi word dma read using DMA handshaking */
  661. *R_ATA_TRANSFER_CNT =
  662. IO_FIELD(R_ATA_TRANSFER_CNT, count, ata_tot_size >> 1);
  663. *R_ATA_CTRL_DATA =
  664. IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
  665. IO_STATE(R_ATA_CTRL_DATA, rw, read) |
  666. IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) |
  667. IO_STATE(R_ATA_CTRL_DATA, handsh, dma) |
  668. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  669. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  670. LED_DISK_READ(1);
  671. D(printk("dma read of %d bytes.\n", ata_tot_size));
  672. } else {
  673. /* writing */
  674. /* begin DMA */
  675. *R_DMA_CH2_FIRST = virt_to_phys(ata_descrs);
  676. *R_DMA_CH2_CMD = IO_STATE(R_DMA_CH2_CMD, cmd, start);
  677. /* initiate a multi word dma write using DMA handshaking */
  678. *R_ATA_TRANSFER_CNT =
  679. IO_FIELD(R_ATA_TRANSFER_CNT, count, ata_tot_size >> 1);
  680. *R_ATA_CTRL_DATA =
  681. IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
  682. IO_STATE(R_ATA_CTRL_DATA, rw, write) |
  683. IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) |
  684. IO_STATE(R_ATA_CTRL_DATA, handsh, dma) |
  685. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  686. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  687. LED_DISK_WRITE(1);
  688. D(printk("dma write of %d bytes.\n", ata_tot_size));
  689. }
  690. }