ide-cris.c 27 KB

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