ide-cris.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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_PB7_RESET
  366. port_pb_dir_shadow = port_pb_dir_shadow |
  367. IO_STATE(R_PORT_PB_DIR, dir7, output);
  368. *R_PORT_PB_DIR = port_pb_dir_shadow;
  369. REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 7, val);
  370. #endif
  371. }
  372. static __init void
  373. cris_ide_init(void)
  374. {
  375. volatile unsigned int dummy;
  376. *R_ATA_CTRL_DATA = 0;
  377. *R_ATA_TRANSFER_CNT = 0;
  378. *R_ATA_CONFIG = 0;
  379. if (cris_request_io_interface(if_ata, "ETRAX100LX IDE")) {
  380. printk(KERN_CRIT "ide: Failed to get IO interface\n");
  381. return;
  382. } else if (cris_request_dma(ATA_TX_DMA_NBR,
  383. "ETRAX100LX IDE TX",
  384. DMA_VERBOSE_ON_ERROR,
  385. dma_ata)) {
  386. cris_free_io_interface(if_ata);
  387. printk(KERN_CRIT "ide: Failed to get Tx DMA channel\n");
  388. return;
  389. } else if (cris_request_dma(ATA_RX_DMA_NBR,
  390. "ETRAX100LX IDE RX",
  391. DMA_VERBOSE_ON_ERROR,
  392. dma_ata)) {
  393. cris_free_dma(ATA_TX_DMA_NBR, "ETRAX100LX IDE Tx");
  394. cris_free_io_interface(if_ata);
  395. printk(KERN_CRIT "ide: Failed to get Rx DMA channel\n");
  396. return;
  397. }
  398. /* make a dummy read to set the ata controller in a proper state */
  399. dummy = *R_ATA_STATUS_DATA;
  400. *R_ATA_CONFIG = ( IO_FIELD( R_ATA_CONFIG, enable, 1 ));
  401. *R_ATA_CTRL_DATA = ( IO_STATE( R_ATA_CTRL_DATA, rw, read) |
  402. IO_FIELD( R_ATA_CTRL_DATA, addr, 1 ) );
  403. while(*R_ATA_STATUS_DATA & IO_MASK(R_ATA_STATUS_DATA, busy)); /* wait for busy flag*/
  404. *R_IRQ_MASK0_SET = ( IO_STATE( R_IRQ_MASK0_SET, ata_irq0, set ) |
  405. IO_STATE( R_IRQ_MASK0_SET, ata_irq1, set ) |
  406. IO_STATE( R_IRQ_MASK0_SET, ata_irq2, set ) |
  407. IO_STATE( R_IRQ_MASK0_SET, ata_irq3, set ) );
  408. /* reset the dma channels we will use */
  409. RESET_DMA(ATA_TX_DMA_NBR);
  410. RESET_DMA(ATA_RX_DMA_NBR);
  411. WAIT_DMA(ATA_TX_DMA_NBR);
  412. WAIT_DMA(ATA_RX_DMA_NBR);
  413. }
  414. #define cris_dma_descr_type etrax_dma_descr
  415. #define cris_pio_read IO_STATE(R_ATA_CTRL_DATA, rw, read)
  416. #define cris_ultra_mask 0x0
  417. #define MAX_DESCR_SIZE 0x10000UL
  418. static unsigned long
  419. cris_ide_get_reg(unsigned long reg)
  420. {
  421. return (reg & 0x0e000000) >> 25;
  422. }
  423. static void
  424. cris_ide_fill_descriptor(cris_dma_descr_type *d, void* buf, unsigned int len, int last)
  425. {
  426. d->buf = virt_to_phys(buf);
  427. d->sw_len = len == MAX_DESCR_SIZE ? 0 : len;
  428. if (last)
  429. d->ctrl |= d_eol;
  430. }
  431. static void cris_ide_start_dma(ide_drive_t *drive, cris_dma_descr_type *d, int dir, int type, int len)
  432. {
  433. unsigned long cmd;
  434. if (dir) {
  435. /* need to do this before RX DMA due to a chip bug
  436. * it is enough to just flush the part of the cache that
  437. * corresponds to the buffers we start, but since HD transfers
  438. * usually are more than 8 kB, it is easier to optimize for the
  439. * normal case and just flush the entire cache. its the only
  440. * way to be sure! (OB movie quote)
  441. */
  442. flush_etrax_cache();
  443. *R_DMA_CH3_FIRST = virt_to_phys(d);
  444. *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start);
  445. } else {
  446. *R_DMA_CH2_FIRST = virt_to_phys(d);
  447. *R_DMA_CH2_CMD = IO_STATE(R_DMA_CH2_CMD, cmd, start);
  448. }
  449. /* initiate a multi word dma read using DMA handshaking */
  450. *R_ATA_TRANSFER_CNT =
  451. IO_FIELD(R_ATA_TRANSFER_CNT, count, len >> 1);
  452. cmd = dir ? IO_STATE(R_ATA_CTRL_DATA, rw, read) : IO_STATE(R_ATA_CTRL_DATA, rw, write);
  453. cmd |= type == TYPE_PIO ? IO_STATE(R_ATA_CTRL_DATA, handsh, pio) :
  454. IO_STATE(R_ATA_CTRL_DATA, handsh, dma);
  455. *R_ATA_CTRL_DATA =
  456. cmd |
  457. IO_FIELD(R_ATA_CTRL_DATA, data, IDE_DATA_REG) |
  458. IO_STATE(R_ATA_CTRL_DATA, src_dst, dma) |
  459. IO_STATE(R_ATA_CTRL_DATA, multi, on) |
  460. IO_STATE(R_ATA_CTRL_DATA, dma_size, word);
  461. }
  462. static void
  463. cris_ide_wait_dma(int dir)
  464. {
  465. if (dir)
  466. WAIT_DMA(ATA_RX_DMA_NBR);
  467. else
  468. WAIT_DMA(ATA_TX_DMA_NBR);
  469. }
  470. static int cris_dma_test_irq(ide_drive_t *drive)
  471. {
  472. int intr = *R_IRQ_MASK0_RD;
  473. int bus = IO_EXTRACT(R_ATA_CTRL_DATA, sel, IDE_DATA_REG);
  474. return intr & (1 << (bus + IO_BITNR(R_IRQ_MASK0_RD, ata_irq0))) ? 1 : 0;
  475. }
  476. static void cris_ide_initialize_dma(int dir)
  477. {
  478. if (dir)
  479. {
  480. RESET_DMA(ATA_RX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
  481. WAIT_DMA(ATA_RX_DMA_NBR);
  482. }
  483. else
  484. {
  485. RESET_DMA(ATA_TX_DMA_NBR); /* sometimes the DMA channel get stuck so we need to do this */
  486. WAIT_DMA(ATA_TX_DMA_NBR);
  487. }
  488. }
  489. #endif
  490. void
  491. cris_ide_outw(unsigned short data, unsigned long reg) {
  492. int timeleft;
  493. LOWDB(printk("ow: data 0x%x, reg 0x%x\n", data, reg));
  494. /* note the lack of handling any timeouts. we stop waiting, but we don't
  495. * really notify anybody.
  496. */
  497. timeleft = IDE_REGISTER_TIMEOUT;
  498. /* wait for busy flag */
  499. do {
  500. timeleft--;
  501. } while(timeleft && cris_ide_busy());
  502. /*
  503. * Fall through at a timeout, so the ongoing command will be
  504. * aborted by the write below, which is expected to be a dummy
  505. * command to the command register. This happens when a faulty
  506. * drive times out on a command. See comment on timeout in
  507. * INB.
  508. */
  509. if(!timeleft)
  510. printk("ATA timeout reg 0x%lx := 0x%x\n", reg, data);
  511. cris_ide_write_command(reg|data); /* write data to the drive's register */
  512. timeleft = IDE_REGISTER_TIMEOUT;
  513. /* wait for transmitter ready */
  514. do {
  515. timeleft--;
  516. } while(timeleft && !cris_ide_ready());
  517. }
  518. void
  519. cris_ide_outb(unsigned char data, unsigned long reg)
  520. {
  521. cris_ide_outw(data, reg);
  522. }
  523. void
  524. cris_ide_outbsync(ide_drive_t *drive, u8 addr, unsigned long port)
  525. {
  526. cris_ide_outw(addr, port);
  527. }
  528. unsigned short
  529. cris_ide_inw(unsigned long reg) {
  530. int timeleft;
  531. unsigned short val;
  532. timeleft = IDE_REGISTER_TIMEOUT;
  533. /* wait for busy flag */
  534. do {
  535. timeleft--;
  536. } while(timeleft && cris_ide_busy());
  537. if(!timeleft) {
  538. /*
  539. * If we're asked to read the status register, like for
  540. * example when a command does not complete for an
  541. * extended time, but the ATA interface is stuck in a
  542. * busy state at the *ETRAX* ATA interface level (as has
  543. * happened repeatedly with at least one bad disk), then
  544. * the best thing to do is to pretend that we read
  545. * "busy" in the status register, so the IDE driver will
  546. * time-out, abort the ongoing command and perform a
  547. * reset sequence. Note that the subsequent OUT_BYTE
  548. * call will also timeout on busy, but as long as the
  549. * write is still performed, everything will be fine.
  550. */
  551. if (cris_ide_get_reg(reg) == IDE_STATUS_OFFSET)
  552. return BUSY_STAT;
  553. else
  554. /* For other rare cases we assume 0 is good enough. */
  555. return 0;
  556. }
  557. cris_ide_write_command(reg | cris_pio_read);
  558. timeleft = IDE_REGISTER_TIMEOUT;
  559. /* wait for available */
  560. do {
  561. timeleft--;
  562. } while(timeleft && !cris_ide_data_available(&val));
  563. if(!timeleft)
  564. return 0;
  565. LOWDB(printk("inb: 0x%x from reg 0x%x\n", val & 0xff, reg));
  566. return val;
  567. }
  568. unsigned char
  569. cris_ide_inb(unsigned long reg)
  570. {
  571. return (unsigned char)cris_ide_inw(reg);
  572. }
  573. static int cris_dma_end (ide_drive_t *drive);
  574. static int cris_dma_setup (ide_drive_t *drive);
  575. static void cris_dma_exec_cmd (ide_drive_t *drive, u8 command);
  576. static int cris_dma_test_irq(ide_drive_t *drive);
  577. static void cris_dma_start(ide_drive_t *drive);
  578. static void cris_ide_input_data (ide_drive_t *drive, void *, unsigned int);
  579. static void cris_ide_output_data (ide_drive_t *drive, void *, unsigned int);
  580. static void cris_atapi_input_bytes(ide_drive_t *drive, void *, unsigned int);
  581. static void cris_atapi_output_bytes(ide_drive_t *drive, void *, unsigned int);
  582. static void cris_dma_host_set(ide_drive_t *drive, int on)
  583. {
  584. }
  585. static void cris_set_pio_mode(ide_drive_t *drive, const u8 pio)
  586. {
  587. int setup, strobe, hold;
  588. switch(pio)
  589. {
  590. case 0:
  591. setup = ATA_PIO0_SETUP;
  592. strobe = ATA_PIO0_STROBE;
  593. hold = ATA_PIO0_HOLD;
  594. break;
  595. case 1:
  596. setup = ATA_PIO1_SETUP;
  597. strobe = ATA_PIO1_STROBE;
  598. hold = ATA_PIO1_HOLD;
  599. break;
  600. case 2:
  601. setup = ATA_PIO2_SETUP;
  602. strobe = ATA_PIO2_STROBE;
  603. hold = ATA_PIO2_HOLD;
  604. break;
  605. case 3:
  606. setup = ATA_PIO3_SETUP;
  607. strobe = ATA_PIO3_STROBE;
  608. hold = ATA_PIO3_HOLD;
  609. break;
  610. case 4:
  611. setup = ATA_PIO4_SETUP;
  612. strobe = ATA_PIO4_STROBE;
  613. hold = ATA_PIO4_HOLD;
  614. break;
  615. default:
  616. return;
  617. }
  618. cris_ide_set_speed(TYPE_PIO, setup, strobe, hold);
  619. }
  620. static void cris_set_dma_mode(ide_drive_t *drive, const u8 speed)
  621. {
  622. int cyc = 0, dvs = 0, strobe = 0, hold = 0;
  623. switch(speed)
  624. {
  625. case XFER_UDMA_0:
  626. cyc = ATA_UDMA0_CYC;
  627. dvs = ATA_UDMA0_DVS;
  628. break;
  629. case XFER_UDMA_1:
  630. cyc = ATA_UDMA1_CYC;
  631. dvs = ATA_UDMA1_DVS;
  632. break;
  633. case XFER_UDMA_2:
  634. cyc = ATA_UDMA2_CYC;
  635. dvs = ATA_UDMA2_DVS;
  636. break;
  637. case XFER_MW_DMA_0:
  638. strobe = ATA_DMA0_STROBE;
  639. hold = ATA_DMA0_HOLD;
  640. break;
  641. case XFER_MW_DMA_1:
  642. strobe = ATA_DMA1_STROBE;
  643. hold = ATA_DMA1_HOLD;
  644. break;
  645. case XFER_MW_DMA_2:
  646. strobe = ATA_DMA2_STROBE;
  647. hold = ATA_DMA2_HOLD;
  648. break;
  649. }
  650. if (speed >= XFER_UDMA_0)
  651. cris_ide_set_speed(TYPE_UDMA, cyc, dvs, 0);
  652. else
  653. cris_ide_set_speed(TYPE_DMA, 0, strobe, hold);
  654. }
  655. void __init
  656. init_e100_ide (void)
  657. {
  658. hw_regs_t hw;
  659. int ide_offsets[IDE_NR_PORTS];
  660. int h;
  661. int i;
  662. printk("ide: ETRAX FS built-in ATA DMA controller\n");
  663. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
  664. ide_offsets[i] = cris_ide_reg_addr(i, 0, 1);
  665. /* the IDE control register is at ATA address 6, with CS1 active instead of CS0 */
  666. ide_offsets[IDE_CONTROL_OFFSET] = cris_ide_reg_addr(6, 1, 0);
  667. for (h = 0; h < 4; h++) {
  668. ide_hwif_t *hwif = NULL;
  669. ide_setup_ports(&hw, cris_ide_base_address(h),
  670. ide_offsets,
  671. 0, 0, cris_ide_ack_intr,
  672. ide_default_irq(0));
  673. ide_register_hw(&hw, NULL, 1, &hwif);
  674. if (hwif == NULL)
  675. continue;
  676. hwif->mmio = 1;
  677. hwif->chipset = ide_etrax100;
  678. hwif->set_pio_mode = &cris_set_pio_mode;
  679. hwif->set_dma_mode = &cris_set_dma_mode;
  680. hwif->ata_input_data = &cris_ide_input_data;
  681. hwif->ata_output_data = &cris_ide_output_data;
  682. hwif->atapi_input_bytes = &cris_atapi_input_bytes;
  683. hwif->atapi_output_bytes = &cris_atapi_output_bytes;
  684. hwif->dma_host_set = &cris_dma_host_set;
  685. hwif->ide_dma_end = &cris_dma_end;
  686. hwif->dma_setup = &cris_dma_setup;
  687. hwif->dma_exec_cmd = &cris_dma_exec_cmd;
  688. hwif->ide_dma_test_irq = &cris_dma_test_irq;
  689. hwif->dma_start = &cris_dma_start;
  690. hwif->OUTB = &cris_ide_outb;
  691. hwif->OUTW = &cris_ide_outw;
  692. hwif->OUTBSYNC = &cris_ide_outbsync;
  693. hwif->INB = &cris_ide_inb;
  694. hwif->INW = &cris_ide_inw;
  695. hwif->cbl = ATA_CBL_PATA40;
  696. hwif->host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
  697. hwif->pio_mask = ATA_PIO4,
  698. hwif->drives[0].autotune = 1;
  699. hwif->drives[1].autotune = 1;
  700. hwif->ultra_mask = cris_ultra_mask;
  701. hwif->mwdma_mask = 0x07; /* Multiword DMA 0-2 */
  702. }
  703. /* Reset pulse */
  704. cris_ide_reset(0);
  705. udelay(25);
  706. cris_ide_reset(1);
  707. cris_ide_init();
  708. cris_ide_set_speed(TYPE_PIO, ATA_PIO4_SETUP, ATA_PIO4_STROBE, ATA_PIO4_HOLD);
  709. cris_ide_set_speed(TYPE_DMA, 0, ATA_DMA2_STROBE, ATA_DMA2_HOLD);
  710. cris_ide_set_speed(TYPE_UDMA, ATA_UDMA2_CYC, ATA_UDMA2_DVS, 0);
  711. }
  712. static cris_dma_descr_type mydescr __attribute__ ((__aligned__(16)));
  713. /*
  714. * The following routines are mainly used by the ATAPI drivers.
  715. *
  716. * These routines will round up any request for an odd number of bytes,
  717. * so if an odd bytecount is specified, be sure that there's at least one
  718. * extra byte allocated for the buffer.
  719. */
  720. static void
  721. cris_atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
  722. {
  723. D(printk("atapi_input_bytes, buffer 0x%x, count %d\n",
  724. buffer, bytecount));
  725. if(bytecount & 1) {
  726. printk("warning, odd bytecount in cdrom_in_bytes = %d.\n", bytecount);
  727. bytecount++; /* to round off */
  728. }
  729. /* setup DMA and start transfer */
  730. cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
  731. cris_ide_start_dma(drive, &mydescr, 1, TYPE_PIO, bytecount);
  732. /* wait for completion */
  733. LED_DISK_READ(1);
  734. cris_ide_wait_dma(1);
  735. LED_DISK_READ(0);
  736. }
  737. static void
  738. cris_atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
  739. {
  740. D(printk("atapi_output_bytes, buffer 0x%x, count %d\n",
  741. buffer, bytecount));
  742. if(bytecount & 1) {
  743. printk("odd bytecount %d in atapi_out_bytes!\n", bytecount);
  744. bytecount++;
  745. }
  746. cris_ide_fill_descriptor(&mydescr, buffer, bytecount, 1);
  747. cris_ide_start_dma(drive, &mydescr, 0, TYPE_PIO, bytecount);
  748. /* wait for completion */
  749. LED_DISK_WRITE(1);
  750. LED_DISK_READ(1);
  751. cris_ide_wait_dma(0);
  752. LED_DISK_WRITE(0);
  753. }
  754. /*
  755. * This is used for most PIO data transfers *from* the IDE interface
  756. */
  757. static void
  758. cris_ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
  759. {
  760. cris_atapi_input_bytes(drive, buffer, wcount << 2);
  761. }
  762. /*
  763. * This is used for most PIO data transfers *to* the IDE interface
  764. */
  765. static void
  766. cris_ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
  767. {
  768. cris_atapi_output_bytes(drive, buffer, wcount << 2);
  769. }
  770. /* we only have one DMA channel on the chip for ATA, so we can keep these statically */
  771. static cris_dma_descr_type ata_descrs[MAX_DMA_DESCRS] __attribute__ ((__aligned__(16)));
  772. static unsigned int ata_tot_size;
  773. /*
  774. * cris_ide_build_dmatable() prepares a dma request.
  775. * Returns 0 if all went okay, returns 1 otherwise.
  776. */
  777. static int cris_ide_build_dmatable (ide_drive_t *drive)
  778. {
  779. ide_hwif_t *hwif = drive->hwif;
  780. struct scatterlist* sg;
  781. struct request *rq = drive->hwif->hwgroup->rq;
  782. unsigned long size, addr;
  783. unsigned int count = 0;
  784. int i = 0;
  785. sg = hwif->sg_table;
  786. ata_tot_size = 0;
  787. ide_map_sg(drive, rq);
  788. i = hwif->sg_nents;
  789. while(i) {
  790. /*
  791. * Determine addr and size of next buffer area. We assume that
  792. * individual virtual buffers are always composed linearly in
  793. * physical memory. For example, we assume that any 8kB buffer
  794. * is always composed of two adjacent physical 4kB pages rather
  795. * than two possibly non-adjacent physical 4kB pages.
  796. */
  797. /* group sequential buffers into one large buffer */
  798. addr = sg_phys(sg);
  799. size = sg_dma_len(sg);
  800. while (--i) {
  801. sg = sg_next(sg);
  802. if ((addr + size) != sg_phys(sg))
  803. break;
  804. size += sg_dma_len(sg);
  805. }
  806. /* did we run out of descriptors? */
  807. if(count >= MAX_DMA_DESCRS) {
  808. printk("%s: too few DMA descriptors\n", drive->name);
  809. return 1;
  810. }
  811. /* however, this case is more difficult - rw_trf_cnt cannot be more
  812. than 65536 words per transfer, so in that case we need to either
  813. 1) use a DMA interrupt to re-trigger rw_trf_cnt and continue with
  814. the descriptors, or
  815. 2) simply do the request here, and get dma_intr to only ide_end_request on
  816. those blocks that were actually set-up for transfer.
  817. */
  818. if(ata_tot_size + size > 131072) {
  819. printk("too large total ATA DMA request, %d + %d!\n", ata_tot_size, (int)size);
  820. return 1;
  821. }
  822. /* If size > MAX_DESCR_SIZE it has to be splitted into new descriptors. Since we
  823. don't handle size > 131072 only one split is necessary */
  824. if(size > MAX_DESCR_SIZE) {
  825. cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, MAX_DESCR_SIZE, 0);
  826. count++;
  827. ata_tot_size += MAX_DESCR_SIZE;
  828. size -= MAX_DESCR_SIZE;
  829. addr += MAX_DESCR_SIZE;
  830. }
  831. cris_ide_fill_descriptor(&ata_descrs[count], (void*)addr, size,i ? 0 : 1);
  832. count++;
  833. ata_tot_size += size;
  834. }
  835. if (count) {
  836. /* return and say all is ok */
  837. return 0;
  838. }
  839. printk("%s: empty DMA table?\n", drive->name);
  840. return 1; /* let the PIO routines handle this weirdness */
  841. }
  842. /*
  843. * cris_dma_intr() is the handler for disk read/write DMA interrupts
  844. */
  845. static ide_startstop_t cris_dma_intr (ide_drive_t *drive)
  846. {
  847. LED_DISK_READ(0);
  848. LED_DISK_WRITE(0);
  849. return ide_dma_intr(drive);
  850. }
  851. /*
  852. * Functions below initiates/aborts DMA read/write operations on a drive.
  853. *
  854. * The caller is assumed to have selected the drive and programmed the drive's
  855. * sector address using CHS or LBA. All that remains is to prepare for DMA
  856. * and then issue the actual read/write DMA/PIO command to the drive.
  857. *
  858. * For ATAPI devices, we just prepare for DMA and return. The caller should
  859. * then issue the packet command to the drive and call us again with
  860. * cris_dma_start afterwards.
  861. *
  862. * Returns 0 if all went well.
  863. * Returns 1 if DMA read/write could not be started, in which case
  864. * the caller should revert to PIO for the current request.
  865. */
  866. static int cris_dma_end(ide_drive_t *drive)
  867. {
  868. drive->waiting_for_dma = 0;
  869. return 0;
  870. }
  871. static int cris_dma_setup(ide_drive_t *drive)
  872. {
  873. struct request *rq = drive->hwif->hwgroup->rq;
  874. cris_ide_initialize_dma(!rq_data_dir(rq));
  875. if (cris_ide_build_dmatable (drive)) {
  876. ide_map_sg(drive, rq);
  877. return 1;
  878. }
  879. drive->waiting_for_dma = 1;
  880. return 0;
  881. }
  882. static void cris_dma_exec_cmd(ide_drive_t *drive, u8 command)
  883. {
  884. /* set the irq handler which will finish the request when DMA is done */
  885. ide_set_handler(drive, &cris_dma_intr, WAIT_CMD, NULL);
  886. /* issue cmd to drive */
  887. cris_ide_outb(command, IDE_COMMAND_REG);
  888. }
  889. static void cris_dma_start(ide_drive_t *drive)
  890. {
  891. struct request *rq = drive->hwif->hwgroup->rq;
  892. int writing = rq_data_dir(rq);
  893. int type = TYPE_DMA;
  894. if (drive->current_speed >= XFER_UDMA_0)
  895. type = TYPE_UDMA;
  896. cris_ide_start_dma(drive, &ata_descrs[0], writing ? 0 : 1, type, ata_tot_size);
  897. if (writing) {
  898. LED_DISK_WRITE(1);
  899. } else {
  900. LED_DISK_READ(1);
  901. }
  902. }