ide-cris.c 29 KB

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