ide-io-std.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include <linux/kernel.h>
  2. #include <linux/ide.h>
  3. #if defined(CONFIG_ARM) || defined(CONFIG_M68K) || defined(CONFIG_MIPS) || \
  4. defined(CONFIG_PARISC) || defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  5. #include <asm/ide.h>
  6. #else
  7. #include <asm-generic/ide_iops.h>
  8. #endif
  9. /*
  10. * Conventional PIO operations for ATA devices
  11. */
  12. static u8 ide_inb(unsigned long port)
  13. {
  14. return (u8) inb(port);
  15. }
  16. static void ide_outb(u8 val, unsigned long port)
  17. {
  18. outb(val, port);
  19. }
  20. /*
  21. * MMIO operations, typically used for SATA controllers
  22. */
  23. static u8 ide_mm_inb(unsigned long port)
  24. {
  25. return (u8) readb((void __iomem *) port);
  26. }
  27. static void ide_mm_outb(u8 value, unsigned long port)
  28. {
  29. writeb(value, (void __iomem *) port);
  30. }
  31. void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
  32. {
  33. if (hwif->host_flags & IDE_HFLAG_MMIO)
  34. writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
  35. else
  36. outb(cmd, hwif->io_ports.command_addr);
  37. }
  38. EXPORT_SYMBOL_GPL(ide_exec_command);
  39. u8 ide_read_status(ide_hwif_t *hwif)
  40. {
  41. if (hwif->host_flags & IDE_HFLAG_MMIO)
  42. return readb((void __iomem *)hwif->io_ports.status_addr);
  43. else
  44. return inb(hwif->io_ports.status_addr);
  45. }
  46. EXPORT_SYMBOL_GPL(ide_read_status);
  47. u8 ide_read_altstatus(ide_hwif_t *hwif)
  48. {
  49. if (hwif->host_flags & IDE_HFLAG_MMIO)
  50. return readb((void __iomem *)hwif->io_ports.ctl_addr);
  51. else
  52. return inb(hwif->io_ports.ctl_addr);
  53. }
  54. EXPORT_SYMBOL_GPL(ide_read_altstatus);
  55. void ide_set_irq(ide_hwif_t *hwif, int on)
  56. {
  57. u8 ctl = ATA_DEVCTL_OBS;
  58. if (on == 4) { /* hack for SRST */
  59. ctl |= 4;
  60. on &= ~4;
  61. }
  62. ctl |= on ? 0 : 2;
  63. if (hwif->host_flags & IDE_HFLAG_MMIO)
  64. writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
  65. else
  66. outb(ctl, hwif->io_ports.ctl_addr);
  67. }
  68. EXPORT_SYMBOL_GPL(ide_set_irq);
  69. void ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
  70. {
  71. ide_hwif_t *hwif = drive->hwif;
  72. struct ide_io_ports *io_ports = &hwif->io_ports;
  73. struct ide_taskfile *tf = &cmd->tf;
  74. void (*tf_outb)(u8 addr, unsigned long port);
  75. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  76. u8 HIHI = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  77. if (mmio)
  78. tf_outb = ide_mm_outb;
  79. else
  80. tf_outb = ide_outb;
  81. if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
  82. HIHI = 0xFF;
  83. if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) {
  84. u16 data = (tf->hob_data << 8) | tf->data;
  85. if (mmio)
  86. writew(data, (void __iomem *)io_ports->data_addr);
  87. else
  88. outw(data, io_ports->data_addr);
  89. }
  90. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  91. tf_outb(tf->hob_feature, io_ports->feature_addr);
  92. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  93. tf_outb(tf->hob_nsect, io_ports->nsect_addr);
  94. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  95. tf_outb(tf->hob_lbal, io_ports->lbal_addr);
  96. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  97. tf_outb(tf->hob_lbam, io_ports->lbam_addr);
  98. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  99. tf_outb(tf->hob_lbah, io_ports->lbah_addr);
  100. if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
  101. tf_outb(tf->feature, io_ports->feature_addr);
  102. if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
  103. tf_outb(tf->nsect, io_ports->nsect_addr);
  104. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
  105. tf_outb(tf->lbal, io_ports->lbal_addr);
  106. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
  107. tf_outb(tf->lbam, io_ports->lbam_addr);
  108. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
  109. tf_outb(tf->lbah, io_ports->lbah_addr);
  110. if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
  111. tf_outb((tf->device & HIHI) | drive->select,
  112. io_ports->device_addr);
  113. }
  114. EXPORT_SYMBOL_GPL(ide_tf_load);
  115. void ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
  116. {
  117. ide_hwif_t *hwif = drive->hwif;
  118. struct ide_io_ports *io_ports = &hwif->io_ports;
  119. struct ide_taskfile *tf = &cmd->tf;
  120. void (*tf_outb)(u8 addr, unsigned long port);
  121. u8 (*tf_inb)(unsigned long port);
  122. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  123. if (mmio) {
  124. tf_outb = ide_mm_outb;
  125. tf_inb = ide_mm_inb;
  126. } else {
  127. tf_outb = ide_outb;
  128. tf_inb = ide_inb;
  129. }
  130. if (cmd->ftf_flags & IDE_FTFLAG_IN_DATA) {
  131. u16 data;
  132. if (mmio)
  133. data = readw((void __iomem *)io_ports->data_addr);
  134. else
  135. data = inw(io_ports->data_addr);
  136. tf->data = data & 0xff;
  137. tf->hob_data = (data >> 8) & 0xff;
  138. }
  139. /* be sure we're looking at the low order bits */
  140. tf_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
  141. if (cmd->tf_flags & IDE_TFLAG_IN_FEATURE)
  142. tf->feature = tf_inb(io_ports->feature_addr);
  143. if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
  144. tf->nsect = tf_inb(io_ports->nsect_addr);
  145. if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
  146. tf->lbal = tf_inb(io_ports->lbal_addr);
  147. if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
  148. tf->lbam = tf_inb(io_ports->lbam_addr);
  149. if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
  150. tf->lbah = tf_inb(io_ports->lbah_addr);
  151. if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
  152. tf->device = tf_inb(io_ports->device_addr);
  153. if (cmd->tf_flags & IDE_TFLAG_LBA48) {
  154. tf_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
  155. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  156. tf->hob_feature = tf_inb(io_ports->feature_addr);
  157. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  158. tf->hob_nsect = tf_inb(io_ports->nsect_addr);
  159. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  160. tf->hob_lbal = tf_inb(io_ports->lbal_addr);
  161. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  162. tf->hob_lbam = tf_inb(io_ports->lbam_addr);
  163. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  164. tf->hob_lbah = tf_inb(io_ports->lbah_addr);
  165. }
  166. }
  167. EXPORT_SYMBOL_GPL(ide_tf_read);
  168. /*
  169. * Some localbus EIDE interfaces require a special access sequence
  170. * when using 32-bit I/O instructions to transfer data. We call this
  171. * the "vlb_sync" sequence, which consists of three successive reads
  172. * of the sector count register location, with interrupts disabled
  173. * to ensure that the reads all happen together.
  174. */
  175. static void ata_vlb_sync(unsigned long port)
  176. {
  177. (void)inb(port);
  178. (void)inb(port);
  179. (void)inb(port);
  180. }
  181. /*
  182. * This is used for most PIO data transfers *from* the IDE interface
  183. *
  184. * These routines will round up any request for an odd number of bytes,
  185. * so if an odd len is specified, be sure that there's at least one
  186. * extra byte allocated for the buffer.
  187. */
  188. void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  189. unsigned int len)
  190. {
  191. ide_hwif_t *hwif = drive->hwif;
  192. struct ide_io_ports *io_ports = &hwif->io_ports;
  193. unsigned long data_addr = io_ports->data_addr;
  194. u8 io_32bit = drive->io_32bit;
  195. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  196. len++;
  197. if (io_32bit) {
  198. unsigned long uninitialized_var(flags);
  199. if ((io_32bit & 2) && !mmio) {
  200. local_irq_save(flags);
  201. ata_vlb_sync(io_ports->nsect_addr);
  202. }
  203. if (mmio)
  204. __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
  205. else
  206. insl(data_addr, buf, len / 4);
  207. if ((io_32bit & 2) && !mmio)
  208. local_irq_restore(flags);
  209. if ((len & 3) >= 2) {
  210. if (mmio)
  211. __ide_mm_insw((void __iomem *)data_addr,
  212. (u8 *)buf + (len & ~3), 1);
  213. else
  214. insw(data_addr, (u8 *)buf + (len & ~3), 1);
  215. }
  216. } else {
  217. if (mmio)
  218. __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
  219. else
  220. insw(data_addr, buf, len / 2);
  221. }
  222. }
  223. EXPORT_SYMBOL_GPL(ide_input_data);
  224. /*
  225. * This is used for most PIO data transfers *to* the IDE interface
  226. */
  227. void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  228. unsigned int len)
  229. {
  230. ide_hwif_t *hwif = drive->hwif;
  231. struct ide_io_ports *io_ports = &hwif->io_ports;
  232. unsigned long data_addr = io_ports->data_addr;
  233. u8 io_32bit = drive->io_32bit;
  234. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  235. len++;
  236. if (io_32bit) {
  237. unsigned long uninitialized_var(flags);
  238. if ((io_32bit & 2) && !mmio) {
  239. local_irq_save(flags);
  240. ata_vlb_sync(io_ports->nsect_addr);
  241. }
  242. if (mmio)
  243. __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
  244. else
  245. outsl(data_addr, buf, len / 4);
  246. if ((io_32bit & 2) && !mmio)
  247. local_irq_restore(flags);
  248. if ((len & 3) >= 2) {
  249. if (mmio)
  250. __ide_mm_outsw((void __iomem *)data_addr,
  251. (u8 *)buf + (len & ~3), 1);
  252. else
  253. outsw(data_addr, (u8 *)buf + (len & ~3), 1);
  254. }
  255. } else {
  256. if (mmio)
  257. __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
  258. else
  259. outsw(data_addr, buf, len / 2);
  260. }
  261. }
  262. EXPORT_SYMBOL_GPL(ide_output_data);
  263. const struct ide_tp_ops default_tp_ops = {
  264. .exec_command = ide_exec_command,
  265. .read_status = ide_read_status,
  266. .read_altstatus = ide_read_altstatus,
  267. .set_irq = ide_set_irq,
  268. .tf_load = ide_tf_load,
  269. .tf_read = ide_tf_read,
  270. .input_data = ide_input_data,
  271. .output_data = ide_output_data,
  272. };