ide-cris.c 28 KB

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