ide-cris.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /* $Id: cris-ide-driver.patch,v 1.1 2005/06/29 21:39:07 akpm 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-2005 Axis Communications AB
  6. *
  7. * Authors: Bjorn Wesen (initial version)
  8. * Mikael Starvik (crisv32 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/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/timer.h>
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/hdreg.h>
  27. #include <linux/ide.h>
  28. #include <linux/init.h>
  29. #include <asm/io.h>
  30. #include <asm/dma.h>
  31. /* number of DMA descriptors */
  32. #define MAX_DMA_DESCRS 64
  33. /* number of times to retry busy-flags when reading/writing IDE-registers
  34. * this can't be too high because a hung harddisk might cause the watchdog
  35. * to trigger (sometimes INB and OUTB are called with irq's disabled)
  36. */
  37. #define IDE_REGISTER_TIMEOUT 300
  38. #define LOWDB(x)
  39. #define D(x)
  40. enum /* Transfer types */
  41. {
  42. TYPE_PIO,
  43. TYPE_DMA,
  44. TYPE_UDMA
  45. };
  46. /* CRISv32 specifics */
  47. #ifdef CONFIG_ETRAX_ARCH_V32
  48. #include <asm/arch/hwregs/ata_defs.h>
  49. #include <asm/arch/hwregs/dma_defs.h>
  50. #include <asm/arch/hwregs/dma.h>
  51. #include <asm/arch/pinmux.h>
  52. #define ATA_UDMA2_CYC 2
  53. #define ATA_UDMA2_DVS 3
  54. #define ATA_UDMA1_CYC 2
  55. #define ATA_UDMA1_DVS 4
  56. #define ATA_UDMA0_CYC 4
  57. #define ATA_UDMA0_DVS 6
  58. #define ATA_DMA2_STROBE 7
  59. #define ATA_DMA2_HOLD 1
  60. #define ATA_DMA1_STROBE 8
  61. #define ATA_DMA1_HOLD 3
  62. #define ATA_DMA0_STROBE 25
  63. #define ATA_DMA0_HOLD 19
  64. #define ATA_PIO4_SETUP 3
  65. #define ATA_PIO4_STROBE 7
  66. #define ATA_PIO4_HOLD 1
  67. #define ATA_PIO3_SETUP 3
  68. #define ATA_PIO3_STROBE 9
  69. #define ATA_PIO3_HOLD 3
  70. #define ATA_PIO2_SETUP 3
  71. #define ATA_PIO2_STROBE 13
  72. #define ATA_PIO2_HOLD 5
  73. #define ATA_PIO1_SETUP 5
  74. #define ATA_PIO1_STROBE 23
  75. #define ATA_PIO1_HOLD 9
  76. #define ATA_PIO0_SETUP 9
  77. #define ATA_PIO0_STROBE 39
  78. #define ATA_PIO0_HOLD 9
  79. int
  80. cris_ide_ack_intr(ide_hwif_t* hwif)
  81. {
  82. reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2,
  83. int, hwif->io_ports[0]);
  84. REG_WR_INT(ata, regi_ata, rw_ack_intr, 1 << ctrl2.sel);
  85. return 1;
  86. }
  87. static inline int
  88. cris_ide_busy(void)
  89. {
  90. reg_ata_rs_stat_data stat_data;
  91. stat_data = REG_RD(ata, regi_ata, rs_stat_data);
  92. return stat_data.busy;
  93. }
  94. static inline int
  95. cris_ide_ready(void)
  96. {
  97. return !cris_ide_busy();
  98. }
  99. static inline int
  100. cris_ide_data_available(unsigned short* data)
  101. {
  102. reg_ata_rs_stat_data stat_data;
  103. stat_data = REG_RD(ata, regi_ata, rs_stat_data);
  104. *data = stat_data.data;
  105. return stat_data.dav;
  106. }
  107. static void
  108. cris_ide_write_command(unsigned long command)
  109. {
  110. REG_WR_INT(ata, regi_ata, rw_ctrl2, command); /* write data to the drive's register */
  111. }
  112. static void
  113. cris_ide_set_speed(int type, int setup, int strobe, int hold)
  114. {
  115. reg_ata_rw_ctrl0 ctrl0 = REG_RD(ata, regi_ata, rw_ctrl0);
  116. reg_ata_rw_ctrl1 ctrl1 = REG_RD(ata, regi_ata, rw_ctrl1);
  117. if (type == TYPE_PIO) {
  118. ctrl0.pio_setup = setup;
  119. ctrl0.pio_strb = strobe;
  120. ctrl0.pio_hold = hold;
  121. } else if (type == TYPE_DMA) {
  122. ctrl0.dma_strb = strobe;
  123. ctrl0.dma_hold = hold;
  124. } else if (type == TYPE_UDMA) {
  125. ctrl1.udma_tcyc = setup;
  126. ctrl1.udma_tdvs = strobe;
  127. }
  128. REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
  129. REG_WR(ata, regi_ata, rw_ctrl1, ctrl1);
  130. }
  131. static unsigned long
  132. cris_ide_base_address(int bus)
  133. {
  134. reg_ata_rw_ctrl2 ctrl2 = {0};
  135. ctrl2.sel = bus;
  136. return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
  137. }
  138. static unsigned long
  139. cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
  140. {
  141. reg_ata_rw_ctrl2 ctrl2 = {0};
  142. ctrl2.addr = addr;
  143. ctrl2.cs1 = cs1;
  144. ctrl2.cs0 = cs0;
  145. return REG_TYPE_CONV(int, reg_ata_rw_ctrl2, ctrl2);
  146. }
  147. static __init void
  148. cris_ide_reset(unsigned val)
  149. {
  150. reg_ata_rw_ctrl0 ctrl0 = {0};
  151. ctrl0.rst = val ? regk_ata_active : regk_ata_inactive;
  152. REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
  153. }
  154. static __init void
  155. cris_ide_init(void)
  156. {
  157. reg_ata_rw_ctrl0 ctrl0 = {0};
  158. reg_ata_rw_intr_mask intr_mask = {0};
  159. ctrl0.en = regk_ata_yes;
  160. REG_WR(ata, regi_ata, rw_ctrl0, ctrl0);
  161. intr_mask.bus0 = regk_ata_yes;
  162. intr_mask.bus1 = regk_ata_yes;
  163. intr_mask.bus2 = regk_ata_yes;
  164. intr_mask.bus3 = regk_ata_yes;
  165. REG_WR(ata, regi_ata, rw_intr_mask, intr_mask);
  166. crisv32_request_dma(2, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
  167. crisv32_request_dma(3, "ETRAX FS built-in ATA", DMA_VERBOSE_ON_ERROR, 0, dma_ata);
  168. crisv32_pinmux_alloc_fixed(pinmux_ata);
  169. crisv32_pinmux_alloc_fixed(pinmux_ata0);
  170. crisv32_pinmux_alloc_fixed(pinmux_ata1);
  171. crisv32_pinmux_alloc_fixed(pinmux_ata2);
  172. crisv32_pinmux_alloc_fixed(pinmux_ata3);
  173. DMA_RESET(regi_dma2);
  174. DMA_ENABLE(regi_dma2);
  175. DMA_RESET(regi_dma3);
  176. DMA_ENABLE(regi_dma3);
  177. DMA_WR_CMD (regi_dma2, regk_dma_set_w_size2);
  178. DMA_WR_CMD (regi_dma3, regk_dma_set_w_size2);
  179. }
  180. static dma_descr_context mycontext __attribute__ ((__aligned__(32)));
  181. #define cris_dma_descr_type dma_descr_data
  182. #define cris_pio_read regk_ata_rd
  183. #define cris_ultra_mask 0x7
  184. #define MAX_DESCR_SIZE 0xffffffffUL
  185. static unsigned long
  186. cris_ide_get_reg(unsigned long reg)
  187. {
  188. return (reg & 0x0e000000) >> 25;
  189. }
  190. static void
  191. cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
  192. {
  193. d->buf = (char*)virt_to_phys(buf);
  194. d->after = d->buf + len;
  195. d->eol = last;
  196. }
  197. static void
  198. cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir,int type,int len)
  199. {
  200. reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
  201. reg_ata_rw_trf_cnt trf_cnt = {0};
  202. mycontext.saved_data = (dma_descr_data*)virt_to_phys(d);
  203. mycontext.saved_data_buf = d->buf;
  204. /* start the dma channel */
  205. DMA_START_CONTEXT(dir ? regi_dma3 : regi_dma2, virt_to_phys(&mycontext));
  206. /* initiate a multi word dma read using PIO handshaking */
  207. trf_cnt.cnt = len >> 1;
  208. /* Due to a "feature" the transfer count has to be one extra word for UDMA. */
  209. if (type == TYPE_UDMA)
  210. trf_cnt.cnt++;
  211. REG_WR(ata, regi_ata, rw_trf_cnt, trf_cnt);
  212. ctrl2.rw = dir ? regk_ata_rd : regk_ata_wr;
  213. ctrl2.trf_mode = regk_ata_dma;
  214. ctrl2.hsh = type == TYPE_PIO ? regk_ata_pio :
  215. type == TYPE_DMA ? regk_ata_dma : regk_ata_udma;
  216. ctrl2.multi = regk_ata_yes;
  217. ctrl2.dma_size = regk_ata_word;
  218. REG_WR(ata, regi_ata, rw_ctrl2, ctrl2);
  219. }
  220. static void
  221. cris_ide_wait_dma(int dir)
  222. {
  223. reg_dma_rw_stat status;
  224. do
  225. {
  226. status = REG_RD(dma, dir ? regi_dma3 : regi_dma2, rw_stat);
  227. } while(status.list_state != regk_dma_data_at_eol);
  228. }
  229. static int cris_dma_test_irq(ide_drive_t *drive)
  230. {
  231. int intr = REG_RD_INT(ata, regi_ata, r_intr);
  232. reg_ata_rw_ctrl2 ctrl2 = REG_TYPE_CONV(reg_ata_rw_ctrl2, int, IDE_DATA_REG);
  233. return intr & (1 << ctrl2.sel) ? 1 : 0;
  234. }
  235. static void cris_ide_initialize_dma(int dir)
  236. {
  237. }
  238. #else
  239. /* CRISv10 specifics */
  240. #include <asm/arch/svinto.h>
  241. #include <asm/arch/io_interface_mux.h>
  242. /* PIO timing (in R_ATA_CONFIG)
  243. *
  244. * _____________________________
  245. * ADDRESS : ________/
  246. *
  247. * _______________
  248. * DIOR : ____________/ \__________
  249. *
  250. * _______________
  251. * DATA : XXXXXXXXXXXXXXXX_______________XXXXXXXX
  252. *
  253. *
  254. * DIOR is unbuffered while address and data is buffered.
  255. * This creates two problems:
  256. * 1. The DIOR pulse is to early (because it is unbuffered)
  257. * 2. The rise time of DIOR is long
  258. *
  259. * There are at least three different plausible solutions
  260. * 1. Use a pad capable of larger currents in Etrax
  261. * 2. Use an external buffer
  262. * 3. Make the strobe pulse longer
  263. *
  264. * Some of the strobe timings below are modified to compensate
  265. * for this. This implies a slight performance decrease.
  266. *
  267. * THIS SHOULD NEVER BE CHANGED!
  268. *
  269. * TODO: Is this true for the latest LX boards still ?
  270. */
  271. #define ATA_UDMA2_CYC 0 /* No UDMA supported, just to make it compile. */
  272. #define ATA_UDMA2_DVS 0
  273. #define ATA_UDMA1_CYC 0
  274. #define ATA_UDMA1_DVS 0
  275. #define ATA_UDMA0_CYC 0
  276. #define ATA_UDMA0_DVS 0
  277. #define ATA_DMA2_STROBE 4
  278. #define ATA_DMA2_HOLD 0
  279. #define ATA_DMA1_STROBE 4
  280. #define ATA_DMA1_HOLD 1
  281. #define ATA_DMA0_STROBE 12
  282. #define ATA_DMA0_HOLD 9
  283. #define ATA_PIO4_SETUP 1
  284. #define ATA_PIO4_STROBE 5
  285. #define ATA_PIO4_HOLD 0
  286. #define ATA_PIO3_SETUP 1
  287. #define ATA_PIO3_STROBE 5
  288. #define ATA_PIO3_HOLD 1
  289. #define ATA_PIO2_SETUP 1
  290. #define ATA_PIO2_STROBE 6
  291. #define ATA_PIO2_HOLD 2
  292. #define ATA_PIO1_SETUP 2
  293. #define ATA_PIO1_STROBE 11
  294. #define ATA_PIO1_HOLD 4
  295. #define ATA_PIO0_SETUP 4
  296. #define ATA_PIO0_STROBE 19
  297. #define ATA_PIO0_HOLD 4
  298. int
  299. cris_ide_ack_intr(ide_hwif_t* hwif)
  300. {
  301. return 1;
  302. }
  303. static inline int
  304. cris_ide_busy(void)
  305. {
  306. return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy) ;
  307. }
  308. static inline int
  309. cris_ide_ready(void)
  310. {
  311. return *R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, tr_rdy) ;
  312. }
  313. static inline int
  314. cris_ide_data_available(unsigned short* data)
  315. {
  316. unsigned long status = *R_ATA_STATUS_DATA;
  317. *data = (unsigned short)status;
  318. return status & IO_MASK(R_ATA_STATUS_DATA, dav);
  319. }
  320. static void
  321. cris_ide_write_command(unsigned long command)
  322. {
  323. *R_ATA_CTRL_DATA = command;
  324. }
  325. static void
  326. cris_ide_set_speed(int type, int setup, int strobe, int hold)
  327. {
  328. static int pio_setup = ATA_PIO4_SETUP;
  329. static int pio_strobe = ATA_PIO4_STROBE;
  330. static int pio_hold = ATA_PIO4_HOLD;
  331. static int dma_strobe = ATA_DMA2_STROBE;
  332. static int dma_hold = ATA_DMA2_HOLD;
  333. if (type == TYPE_PIO) {
  334. pio_setup = setup;
  335. pio_strobe = strobe;
  336. pio_hold = hold;
  337. } else if (type == TYPE_DMA) {
  338. dma_strobe = strobe;
  339. dma_hold = hold;
  340. }
  341. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ) |
  342. IO_FIELD( R_ATA_CONFIG, dma_strobe, dma_strobe ) |
  343. IO_FIELD( R_ATA_CONFIG, dma_hold, dma_hold ) |
  344. IO_FIELD( R_ATA_CONFIG, pio_setup, pio_setup ) |
  345. IO_FIELD( R_ATA_CONFIG, pio_strobe, pio_strobe ) |
  346. IO_FIELD( R_ATA_CONFIG, pio_hold, pio_hold ) );
  347. }
  348. static unsigned long
  349. cris_ide_base_address(int bus)
  350. {
  351. return IO_FIELD(R_ATA_CTRL_DATA, sel, bus);
  352. }
  353. static unsigned long
  354. cris_ide_reg_addr(unsigned long addr, int cs0, int cs1)
  355. {
  356. return IO_FIELD(R_ATA_CTRL_DATA, addr, addr) |
  357. IO_FIELD(R_ATA_CTRL_DATA, cs0, cs0) |
  358. IO_FIELD(R_ATA_CTRL_DATA, cs1, cs1);
  359. }
  360. static __init void
  361. cris_ide_reset(unsigned val)
  362. {
  363. #ifdef CONFIG_ETRAX_IDE_G27_RESET
  364. REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, 27, val);
  365. #endif
  366. #ifdef CONFIG_ETRAX_IDE_CSE1_16_RESET
  367. REG_SHADOW_SET(port_cse1_addr, port_cse1_shadow, 16, val);
  368. #endif
  369. #ifdef CONFIG_ETRAX_IDE_CSP0_8_RESET
  370. REG_SHADOW_SET(port_csp0_addr, port_csp0_shadow, 8, val);
  371. #endif
  372. #ifdef CONFIG_ETRAX_IDE_PB7_RESET
  373. port_pb_dir_shadow = port_pb_dir_shadow |
  374. IO_STATE(R_PORT_PB_DIR, dir7, output);
  375. *R_PORT_PB_DIR = port_pb_dir_shadow;
  376. REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, val);
  377. #endif
  378. }
  379. static __init void
  380. cris_ide_init(void)
  381. {
  382. volatile unsigned int dummy;
  383. *R_ATA_CTRL_DATA = 0;
  384. *R_ATA_TRANSFER_CNT = 0;
  385. *R_ATA_CONFIG = 0;
  386. if (cris_request_io_interface(if_ata, "ETRAX100LX IDE")) {
  387. printk(KERN_CRIT "ide: Failed to get IO interface\n");
  388. return;
  389. } else if (cris_request_dma(ATA_TX_DMA_NBR,
  390. "ETRAX100LX IDE TX",
  391. DMA_VERBOSE_ON_ERROR,
  392. dma_ata)) {
  393. cris_free_io_interface(if_ata);
  394. printk(KERN_CRIT "ide: Failed to get Tx DMA channel\n");
  395. return;
  396. } else if (cris_request_dma(ATA_RX_DMA_NBR,
  397. "ETRAX100LX IDE RX",
  398. DMA_VERBOSE_ON_ERROR,
  399. dma_ata)) {
  400. cris_free_dma(ATA_TX_DMA_NBR, "ETRAX100LX IDE Tx");
  401. cris_free_io_interface(if_ata);
  402. printk(KERN_CRIT "ide: Failed to get Rx DMA channel\n");
  403. return;
  404. }
  405. /* make a dummy read to set the ata controller in a proper state */
  406. dummy = *R_ATA_STATUS_DATA;
  407. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ));
  408. *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw, read) |
  409. IO_FIELD( R_ATA_CTRL_DATA, addr, 1 ) );
  410. while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/
  411. *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) |
  412. IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) |
  413. IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) |
  414. IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) );
  415. /* reset the dma channels we will use */
  416. RESET_DMA(ATA_TX_DMA_NBR);
  417. RESET_DMA(ATA_RX_DMA_NBR);
  418. WAIT_DMA(ATA_TX_DMA_NBR);
  419. WAIT_DMA(ATA_RX_DMA_NBR);
  420. }
  421. #define cris_dma_descr_type etrax_dma_descr
  422. #define cris_pio_read IO_STATE(R_ATA_CTRL_DATA, rw, read)
  423. #define cris_ultra_mask 0x0
  424. #define MAX_DESCR_SIZE 0x10000UL
  425. static unsigned long
  426. cris_ide_get_reg(unsigned long reg)
  427. {
  428. return (reg & 0x0e000000) >> 25;
  429. }
  430. static void
  431. cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
  432. {
  433. d->buf = virt_to_phys(buf);
  434. d->sw_len = len == MAX_DESCR_SIZE ? 0 : len;
  435. if (last)
  436. d->ctrl |= d_eol;
  437. }
  438. static void cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir, int type, int len)
  439. {
  440. unsigned long cmd;
  441. if (dir) {
  442. /* need to do this before RX DMA due to a chip bug
  443. * it is enough to just flush the part of the cache that
  444. * corresponds to the buffers we start, but since HD transfers
  445. * usually are more than 8 kB, it is easier to optimize for the
  446. * normal case and just flush the entire cache. its the only
  447. * way to be sure! (OB movie quote)
  448. */
  449. flush_etrax_cache();
  450. *R_DMA_CH3_FIRST = virt_to_phys(d);
  451. *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start);
  452. } else {
  453. *R_DMA_CH2_FIRST = virt_to_phys(d);
  454. *R_DMA_CH2_CMD = IO_STATE(R_DMA_CH2_CMD, cmd, start);
  455. }
  456. /* initiate a multi word dma read using DMA handshaking */
  457. *R_ATA_TRANSFER_CNT =
  458. IO_FIELD(R_ATA_TRANSFER_CNT, count, len >> 1);
  459. cmd = dir ? IO_STATE(R_ATA_CTRL_DATA, rw, read) : IO_STATE(R_ATA_CTRL_DATA, rw, write);
  460. cmd |= type == TYPE_PIO ? IO_STATE(R_ATA_CTRL_DATA, handsh, pio) :
  461. IO_STATE(R_ATA_CTRL_DATA, handsh, dma);
  462. *R_ATA_CTRL_DATA =
  463. cmd |
  464. IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
  465. IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) |
  466. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  467. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  468. }
  469. static void
  470. cris_ide_wait_dma(int dir)
  471. {
  472. if (dir)
  473. WAIT_DMA(ATA_RX_DMA_NBR);
  474. else
  475. WAIT_DMA(ATA_TX_DMA_NBR);
  476. }
  477. static int cris_dma_test_irq(ide_drive_t *drive)
  478. {
  479. int intr = *R_IRQ_MASK0_RD;
  480. int bus = IO_EXTRACT(R_ATA_CTRL_DATA, sel, IDE_DATA_REG);
  481. return intr & (1 << (bus + IO_BITNR(R_IRQ_MASK0_RD, ata_irq0))) ? 1 : 0;
  482. }
  483. static void cris_ide_initialize_dma(int dir)
  484. {
  485. if (dir)
  486. {
  487. RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
  488. WAIT_DMA(ATA_RX_DMA_NBR);
  489. }
  490. else
  491. {
  492. RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
  493. WAIT_DMA(ATA_TX_DMA_NBR);
  494. }
  495. }
  496. #endif
  497. void
  498. cris_ide_outw(unsigned short data, unsigned long reg) {
  499. int timeleft;
  500. LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg));
  501. /* note the lack of handling any timeouts. we stop waiting, but we don't
  502. * really notify anybody.
  503. */
  504. timeleft = IDE_REGISTER_TIMEOUT;
  505. /* wait for busy flag */
  506. do {
  507. timeleft--;
  508. } while(timeleft && cris_ide_busy());
  509. /*
  510. * Fall through at a timeout, so the ongoing command will be
  511. * aborted by the write below, which is expected to be a dummy
  512. * command to the command register. This happens when a faulty
  513. * drive times out on a command. See comment on timeout in
  514. * INB.
  515. */
  516. if(!timeleft)
  517. printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data);
  518. cris_ide_write_command(reg|data); /* write data to the drive's register */
  519. timeleft = IDE_REGISTER_TIMEOUT;
  520. /* wait for transmitter ready */
  521. do {
  522. timeleft--;
  523. } while(timeleft && !cris_ide_ready());
  524. }
  525. void
  526. cris_ide_outb(unsigned char data, unsigned long reg)
  527. {
  528. cris_ide_outw(data, reg);
  529. }
  530. void
  531. cris_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port)
  532. {
  533. cris_ide_outw(addr, port);
  534. }
  535. unsigned short
  536. cris_ide_inw(unsigned long reg) {
  537. int timeleft;
  538. unsigned short val;
  539. timeleft = IDE_REGISTER_TIMEOUT;
  540. /* wait for busy flag */
  541. do {
  542. timeleft--;
  543. } while(timeleft && cris_ide_busy());
  544. if(!timeleft) {
  545. /*
  546. * If we're asked to read the status register, like for
  547. * example when a command does not complete for an
  548. * extended time, but the ATA interface is stuck in a
  549. * busy state at the *ETRAX* ATA interface level (as has
  550. * happened repeatedly with at least one bad disk), then
  551. * the best thing to do is to pretend that we read
  552. * "busy" in the status register, so the IDE driver will
  553. * time-out, abort the ongoing command and perform a
  554. * reset sequence. Note that the subsequent OUT_BYTE
  555. * call will also timeout on busy, but as long as the
  556. * write is still performed, everything will be fine.
  557. */
  558. if (cris_ide_get_reg(reg) == IDE_STATUS_OFFSET)
  559. return BUSY_STAT;
  560. else
  561. /* For other rare cases we assume 0 is good enough. */
  562. return 0;
  563. }
  564. cris_ide_write_command(reg | cris_pio_read);
  565. timeleft = IDE_REGISTER_TIMEOUT;
  566. /* wait for available */
  567. do {
  568. timeleft--;
  569. } while(timeleft && !cris_ide_data_available(&val));
  570. if(!timeleft)
  571. return 0;
  572. LOWDB(printk("inb: 0x%x from reg 0x%x\n", val & 0xff, reg));
  573. return val;
  574. }
  575. unsigned char
  576. cris_ide_inb(unsigned long reg)
  577. {
  578. return (unsigned char)cris_ide_inw(reg);
  579. }
  580. static int cris_dma_check (ide_drive_t *drive);
  581. static int cris_dma_end (ide_drive_t *drive);
  582. static int cris_dma_setup (ide_drive_t *drive);
  583. static void cris_dma_exec_cmd (ide_drive_t *drive, u8 command);
  584. static int cris_dma_test_irq(ide_drive_t *drive);
  585. static void cris_dma_start(ide_drive_t *drive);
  586. static void cris_ide_input_data (ide_drive_t *drive, void *, unsigned int);
  587. static void cris_ide_output_data (ide_drive_t *drive, void *, unsigned int);
  588. static void cris_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);
  589. static void cris_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);
  590. static int cris_dma_off (ide_drive_t *drive);
  591. static int cris_dma_on (ide_drive_t *drive);
  592. static void tune_cris_ide(ide_drive_t *drive, u8 pio)
  593. {
  594. int setup, strobe, hold;
  595. switch(pio)
  596. {
  597. case 0:
  598. setup = ATA_PIO0_SETUP;
  599. strobe = ATA_PIO0_STROBE;
  600. hold = ATA_PIO0_HOLD;
  601. break;
  602. case 1:
  603. setup = ATA_PIO1_SETUP;
  604. strobe = ATA_PIO1_STROBE;
  605. hold = ATA_PIO1_HOLD;
  606. break;
  607. case 2:
  608. setup = ATA_PIO2_SETUP;
  609. strobe = ATA_PIO2_STROBE;
  610. hold = ATA_PIO2_HOLD;
  611. break;
  612. case 3:
  613. setup = ATA_PIO3_SETUP;
  614. strobe = ATA_PIO3_STROBE;
  615. hold = ATA_PIO3_HOLD;
  616. break;
  617. case 4:
  618. setup = ATA_PIO4_SETUP;
  619. strobe = ATA_PIO4_STROBE;
  620. hold = ATA_PIO4_HOLD;
  621. break;
  622. default:
  623. return;
  624. }
  625. cris_ide_set_speed(TYPE_PIO, setup, strobe, hold);
  626. }
  627. static int speed_cris_ide(ide_drive_t *drive, u8 speed)
  628. {
  629. int cyc = 0, dvs = 0, strobe = 0, hold = 0;
  630. if (speed >= XFER_PIO_0 && speed <= XFER_PIO_4) {
  631. tune_cris_ide(drive, speed - XFER_PIO_0);
  632. return 0;
  633. }
  634. switch(speed)
  635. {
  636. case XFER_UDMA_0:
  637. cyc = ATA_UDMA0_CYC;
  638. dvs = ATA_UDMA0_DVS;
  639. break;
  640. case XFER_UDMA_1:
  641. cyc = ATA_UDMA1_CYC;
  642. dvs = ATA_UDMA1_DVS;
  643. break;
  644. case XFER_UDMA_2:
  645. cyc = ATA_UDMA2_CYC;
  646. dvs = ATA_UDMA2_DVS;
  647. break;
  648. case XFER_MW_DMA_0:
  649. strobe = ATA_DMA0_STROBE;
  650. hold = ATA_DMA0_HOLD;
  651. break;
  652. case XFER_MW_DMA_1:
  653. strobe = ATA_DMA1_STROBE;
  654. hold = ATA_DMA1_HOLD;
  655. break;
  656. case XFER_MW_DMA_2:
  657. strobe = ATA_DMA2_STROBE;
  658. hold = ATA_DMA2_HOLD;
  659. break;
  660. default:
  661. return 0;
  662. }
  663. if (speed >= XFER_UDMA_0)
  664. cris_ide_set_speed(TYPE_UDMA, cyc, dvs, 0);
  665. else
  666. cris_ide_set_speed(TYPE_DMA, 0, strobe, hold);
  667. return 0;
  668. }
  669. void __init
  670. init_e100_ide (void)
  671. {
  672. hw_regs_t hw;
  673. int ide_offsets[IDE_NR_PORTS];
  674. int h;
  675. int i;
  676. printk("ide: ETRAX FS built-in ATA DMA controller\n");
  677. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
  678. ide_offsets[i] = cris_ide_reg_addr(i, 0, 1);
  679. /* the IDE control register is at ATA address 6, with CS1 active instead of CS0 */
  680. ide_offsets[IDE_CONTROL_OFFSET] = cris_ide_reg_addr(6, 1, 0);
  681. /* first fill in some stuff in the ide_hwifs fields */
  682. for(h = 0; h < MAX_HWIFS; h++) {
  683. ide_hwif_t *hwif = &ide_hwifs[h];
  684. ide_setup_ports(&hw, cris_ide_base_address(h),
  685. ide_offsets,
  686. 0, 0, cris_ide_ack_intr,
  687. ide_default_irq(0));
  688. ide_register_hw(&hw, &hwif);
  689. hwif->mmio = 1;
  690. hwif->chipset = ide_etrax100;
  691. hwif->tuneproc = &tune_cris_ide;
  692. hwif->speedproc = &speed_cris_ide;
  693. hwif->ata_input_data = &cris_ide_input_data;
  694. hwif->ata_output_data = &cris_ide_output_data;
  695. hwif->atapi_input_bytes = &cris_atapi_input_bytes;
  696. hwif->atapi_output_bytes = &cris_atapi_output_bytes;
  697. hwif->ide_dma_check = &cris_dma_check;
  698. hwif->ide_dma_end = &cris_dma_end;
  699. hwif->dma_setup = &cris_dma_setup;
  700. hwif->dma_exec_cmd = &cris_dma_exec_cmd;
  701. hwif->ide_dma_test_irq = &cris_dma_test_irq;
  702. hwif->dma_start = &cris_dma_start;
  703. hwif->OUTB = &cris_ide_outb;
  704. hwif->OUTW = &cris_ide_outw;
  705. hwif->OUTBSYNC = &cris_ide_outbsync;
  706. hwif->INB = &cris_ide_inb;
  707. hwif->INW = &cris_ide_inw;
  708. hwif->ide_dma_host_off = &cris_dma_off;
  709. hwif->ide_dma_host_on = &cris_dma_on;
  710. hwif->ide_dma_off_quietly = &cris_dma_off;
  711. hwif->udma_four = 0;
  712. hwif->ultra_mask = cris_ultra_mask;
  713. hwif->mwdma_mask = 0x07; /* Multiword DMA 0-2 */
  714. hwif->swdma_mask = 0x07; /* Singleword DMA 0-2 */
  715. hwif->autodma = 1;
  716. hwif->drives[0].autodma = 1;
  717. hwif->drives[1].autodma = 1;
  718. }
  719. /* Reset pulse */
  720. cris_ide_reset(0);
  721. udelay(25);
  722. cris_ide_reset(1);
  723. cris_ide_init();
  724. cris_ide_set_speed(TYPE_PIO, ATA_PIO4_SETUP, ATA_PIO4_STROBE, ATA_PIO4_HOLD);
  725. cris_ide_set_speed(TYPE_DMA, 0, ATA_DMA2_STROBE, ATA_DMA2_HOLD);
  726. cris_ide_set_speed(TYPE_UDMA, ATA_UDMA2_CYC, ATA_UDMA2_DVS, 0);
  727. }
  728. static int cris_dma_off (ide_drive_t *drive)
  729. {
  730. return 0;
  731. }
  732. static int cris_dma_on (ide_drive_t *drive)
  733. {
  734. return 0;
  735. }
  736. static cris_dma_descr_type mydescr __attribute__ ((__aligned__(16)));
  737. /*
  738. * The following routines are mainly used by the ATAPI drivers.
  739. *
  740. * These routines will round up any request for an odd number of bytes,
  741. * so if an odd bytecount is specified, be sure that there's at least one
  742. * extra byte allocated for the buffer.
  743. */
  744. static void
  745. cris_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
  746. {
  747. D(printk("atapi_input_bytes, buffer 0x%x, count %d\n",
  748. buffer, bytecount));
  749. if(bytecount & 1) {
  750. printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount);
  751. bytecount++; /* to round off */
  752. }
  753. /* setup DMA and start transfer */
  754. cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
  755. cris_ide_start_dma(drive, &mydescr, 1, TYPE_PIO, bytecount);
  756. /* wait for completion */
  757. LED_DISK_READ(1);
  758. cris_ide_wait_dma(1);
  759. LED_DISK_READ(0);
  760. }
  761. static void
  762. cris_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
  763. {
  764. D(printk("atapi_output_bytes, buffer 0x%x, count %d\n",
  765. buffer, bytecount));
  766. if(bytecount & 1) {
  767. printk("odd bytecount %d in atapi_out_bytes!\n", bytecount);
  768. bytecount++;
  769. }
  770. cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
  771. cris_ide_start_dma(drive, &mydescr, 0, TYPE_PIO, bytecount);
  772. /* wait for completion */
  773. LED_DISK_WRITE(1);
  774. LED_DISK_READ(1);
  775. cris_ide_wait_dma(0);
  776. LED_DISK_WRITE(0);
  777. }
  778. /*
  779. * This is used for most PIO data transfers *from* the IDE interface
  780. */
  781. static void
  782. cris_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
  783. {
  784. cris_atapi_input_bytes(drive, buffer, wcount << 2);
  785. }
  786. /*
  787. * This is used for most PIO data transfers *to* the IDE interface
  788. */
  789. static void
  790. cris_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
  791. {
  792. cris_atapi_output_bytes(drive, buffer, wcount << 2);
  793. }
  794. /* we only have one DMA channel on the chip for ATA, so we can keep these statically */
  795. static cris_dma_descr_type ata_descrs[MAX_DMA_DESCRS] __attribute__ ((__aligned__(16)));
  796. static unsigned int ata_tot_size;
  797. /*
  798. * cris_ide_build_dmatable() prepares a dma request.
  799. * Returns 0 if all went okay, returns 1 otherwise.
  800. */
  801. static int cris_ide_build_dmatable (ide_drive_t *drive)
  802. {
  803. ide_hwif_t *hwif = drive->hwif;
  804. struct scatterlist* sg;
  805. struct request *rq = drive->hwif->hwgroup->rq;
  806. unsigned long size, addr;
  807. unsigned int count = 0;
  808. int i = 0;
  809. sg = hwif->sg_table;
  810. ata_tot_size = 0;
  811. ide_map_sg(drive, rq);
  812. i = hwif->sg_nents;
  813. while(i) {
  814. /*
  815. * Determine addr and size of next buffer area. We assume that
  816. * individual virtual buffers are always composed linearly in
  817. * physical memory. For example, we assume that any 8kB buffer
  818. * is always composed of two adjacent physical 4kB pages rather
  819. * than two possibly non-adjacent physical 4kB pages.
  820. */
  821. /* group sequential buffers into one large buffer */
  822. addr = page_to_phys(sg->page) + sg->offset;
  823. size = sg_dma_len(sg);
  824. while (sg++, --i) {
  825. if ((addr + size) != page_to_phys(sg->page) + sg->offset)
  826. break;
  827. size += sg_dma_len(sg);
  828. }
  829. /* did we run out of descriptors? */
  830. if(count >= MAX_DMA_DESCRS) {
  831. printk("%s: too few DMA descriptors\n", drive->name);
  832. return 1;
  833. }
  834. /* however, this case is more difficult - rw_trf_cnt cannot be more
  835. than 65536 words per transfer, so in that case we need to either
  836. 1) use a DMA interrupt to re-trigger rw_trf_cnt and continue with
  837. the descriptors, or
  838. 2) simply do the request here, and get dma_intr to only ide_end_request on
  839. those blocks that were actually set-up for transfer.
  840. */
  841. if(ata_tot_size + size > 131072) {
  842. printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size);
  843. return 1;
  844. }
  845. /* If size > MAX_DESCR_SIZE it has to be splitted into new descriptors. Since we
  846. don't handle size > 131072 only one split is necessary */
  847. if(size > MAX_DESCR_SIZE) {
  848. cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, MAX_DESCR_SIZE, 0);
  849. count++;
  850. ata_tot_size += MAX_DESCR_SIZE;
  851. size -= MAX_DESCR_SIZE;
  852. addr += MAX_DESCR_SIZE;
  853. }
  854. cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, size,i ? 0 : 1);
  855. count++;
  856. ata_tot_size += size;
  857. }
  858. if (count) {
  859. /* return and say all is ok */
  860. return 0;
  861. }
  862. printk("%s: empty DMA table?\n", drive->name);
  863. return 1; /* let the PIO routines handle this weirdness */
  864. }
  865. static int cris_config_drive_for_dma (ide_drive_t *drive)
  866. {
  867. u8 speed = ide_dma_speed(drive, 1);
  868. if (!speed)
  869. return 0;
  870. speed_cris_ide(drive, speed);
  871. ide_config_drive_speed(drive, speed);
  872. return ide_dma_enable(drive);
  873. }
  874. /*
  875. * cris_dma_intr() is the handler for disk read/write DMA interrupts
  876. */
  877. static ide_startstop_t cris_dma_intr (ide_drive_t *drive)
  878. {
  879. LED_DISK_READ(0);
  880. LED_DISK_WRITE(0);
  881. return ide_dma_intr(drive);
  882. }
  883. /*
  884. * Functions below initiates/aborts DMA read/write operations on a drive.
  885. *
  886. * The caller is assumed to have selected the drive and programmed the drive's
  887. * sector address using CHS or LBA. All that remains is to prepare for DMA
  888. * and then issue the actual read/write DMA/PIO command to the drive.
  889. *
  890. * For ATAPI devices, we just prepare for DMA and return. The caller should
  891. * then issue the packet command to the drive and call us again with
  892. * cris_dma_start afterwards.
  893. *
  894. * Returns 0 if all went well.
  895. * Returns 1 if DMA read/write could not be started, in which case
  896. * the caller should revert to PIO for the current request.
  897. */
  898. static int cris_dma_check(ide_drive_t *drive)
  899. {
  900. if (ide_use_dma(drive) && cris_config_drive_for_dma(drive))
  901. return 0;
  902. return -1;
  903. }
  904. static int cris_dma_end(ide_drive_t *drive)
  905. {
  906. drive->waiting_for_dma = 0;
  907. return 0;
  908. }
  909. static int cris_dma_setup(ide_drive_t *drive)
  910. {
  911. struct request *rq = drive->hwif->hwgroup->rq;
  912. cris_ide_initialize_dma(!rq_data_dir(rq));
  913. if (cris_ide_build_dmatable (drive)) {
  914. ide_map_sg(drive, rq);
  915. return 1;
  916. }
  917. drive->waiting_for_dma = 1;
  918. return 0;
  919. }
  920. static void cris_dma_exec_cmd(ide_drive_t *drive, u8 command)
  921. {
  922. /* set the irq handler which will finish the request when DMA is done */
  923. ide_set_handler(drive, &cris_dma_intr, WAIT_CMD, NULL);
  924. /* issue cmd to drive */
  925. cris_ide_outb(command, IDE_COMMAND_REG);
  926. }
  927. static void cris_dma_start(ide_drive_t *drive)
  928. {
  929. struct request *rq = drive->hwif->hwgroup->rq;
  930. int writing = rq_data_dir(rq);
  931. int type = TYPE_DMA;
  932. if (drive->current_speed >= XFER_UDMA_0)
  933. type = TYPE_UDMA;
  934. cris_ide_start_dma(drive, &ata_descrs[0], writing ? 0 : 1, type, ata_tot_size);
  935. if (writing) {
  936. LED_DISK_WRITE(1);
  937. } else {
  938. LED_DISK_READ(1);
  939. }
  940. }