ide-io-std.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_write_devctl(ide_hwif_t *hwif, u8 ctl)
  56. {
  57. if (hwif->host_flags & IDE_HFLAG_MMIO)
  58. writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
  59. else
  60. outb(ctl, hwif->io_ports.ctl_addr);
  61. }
  62. EXPORT_SYMBOL_GPL(ide_write_devctl);
  63. void ide_dev_select(ide_drive_t *drive)
  64. {
  65. ide_hwif_t *hwif = drive->hwif;
  66. u8 select = drive->select | ATA_DEVICE_OBS;
  67. if (hwif->host_flags & IDE_HFLAG_MMIO)
  68. writeb(select, (void __iomem *)hwif->io_ports.device_addr);
  69. else
  70. outb(select, hwif->io_ports.device_addr);
  71. }
  72. EXPORT_SYMBOL_GPL(ide_dev_select);
  73. void ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
  74. {
  75. ide_hwif_t *hwif = drive->hwif;
  76. struct ide_io_ports *io_ports = &hwif->io_ports;
  77. struct ide_taskfile *tf = &cmd->tf;
  78. void (*tf_outb)(u8 addr, unsigned long port);
  79. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  80. u8 HIHI = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  81. if (mmio)
  82. tf_outb = ide_mm_outb;
  83. else
  84. tf_outb = ide_outb;
  85. if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
  86. HIHI = 0xFF;
  87. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  88. tf_outb(tf->hob_feature, io_ports->feature_addr);
  89. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  90. tf_outb(tf->hob_nsect, io_ports->nsect_addr);
  91. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  92. tf_outb(tf->hob_lbal, io_ports->lbal_addr);
  93. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  94. tf_outb(tf->hob_lbam, io_ports->lbam_addr);
  95. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  96. tf_outb(tf->hob_lbah, io_ports->lbah_addr);
  97. if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
  98. tf_outb(tf->feature, io_ports->feature_addr);
  99. if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
  100. tf_outb(tf->nsect, io_ports->nsect_addr);
  101. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
  102. tf_outb(tf->lbal, io_ports->lbal_addr);
  103. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
  104. tf_outb(tf->lbam, io_ports->lbam_addr);
  105. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
  106. tf_outb(tf->lbah, io_ports->lbah_addr);
  107. if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
  108. tf_outb((tf->device & HIHI) | drive->select,
  109. io_ports->device_addr);
  110. }
  111. EXPORT_SYMBOL_GPL(ide_tf_load);
  112. void ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
  113. {
  114. ide_hwif_t *hwif = drive->hwif;
  115. struct ide_io_ports *io_ports = &hwif->io_ports;
  116. struct ide_taskfile *tf = &cmd->tf;
  117. void (*tf_outb)(u8 addr, unsigned long port);
  118. u8 (*tf_inb)(unsigned long port);
  119. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  120. if (mmio) {
  121. tf_outb = ide_mm_outb;
  122. tf_inb = ide_mm_inb;
  123. } else {
  124. tf_outb = ide_outb;
  125. tf_inb = ide_inb;
  126. }
  127. /* be sure we're looking at the low order bits */
  128. tf_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
  129. if (cmd->tf_flags & IDE_TFLAG_IN_ERROR)
  130. tf->error = tf_inb(io_ports->feature_addr);
  131. if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
  132. tf->nsect = tf_inb(io_ports->nsect_addr);
  133. if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
  134. tf->lbal = tf_inb(io_ports->lbal_addr);
  135. if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
  136. tf->lbam = tf_inb(io_ports->lbam_addr);
  137. if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
  138. tf->lbah = tf_inb(io_ports->lbah_addr);
  139. if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
  140. tf->device = tf_inb(io_ports->device_addr);
  141. if (cmd->tf_flags & IDE_TFLAG_LBA48) {
  142. tf_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
  143. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_ERROR)
  144. tf->hob_error = tf_inb(io_ports->feature_addr);
  145. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  146. tf->hob_nsect = tf_inb(io_ports->nsect_addr);
  147. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  148. tf->hob_lbal = tf_inb(io_ports->lbal_addr);
  149. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  150. tf->hob_lbam = tf_inb(io_ports->lbam_addr);
  151. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  152. tf->hob_lbah = tf_inb(io_ports->lbah_addr);
  153. }
  154. }
  155. EXPORT_SYMBOL_GPL(ide_tf_read);
  156. /*
  157. * Some localbus EIDE interfaces require a special access sequence
  158. * when using 32-bit I/O instructions to transfer data. We call this
  159. * the "vlb_sync" sequence, which consists of three successive reads
  160. * of the sector count register location, with interrupts disabled
  161. * to ensure that the reads all happen together.
  162. */
  163. static void ata_vlb_sync(unsigned long port)
  164. {
  165. (void)inb(port);
  166. (void)inb(port);
  167. (void)inb(port);
  168. }
  169. /*
  170. * This is used for most PIO data transfers *from* the IDE interface
  171. *
  172. * These routines will round up any request for an odd number of bytes,
  173. * so if an odd len is specified, be sure that there's at least one
  174. * extra byte allocated for the buffer.
  175. */
  176. void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  177. unsigned int len)
  178. {
  179. ide_hwif_t *hwif = drive->hwif;
  180. struct ide_io_ports *io_ports = &hwif->io_ports;
  181. unsigned long data_addr = io_ports->data_addr;
  182. unsigned int words = (len + 1) >> 1;
  183. u8 io_32bit = drive->io_32bit;
  184. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  185. if (io_32bit) {
  186. unsigned long uninitialized_var(flags);
  187. if ((io_32bit & 2) && !mmio) {
  188. local_irq_save(flags);
  189. ata_vlb_sync(io_ports->nsect_addr);
  190. }
  191. words >>= 1;
  192. if (mmio)
  193. __ide_mm_insl((void __iomem *)data_addr, buf, words);
  194. else
  195. insl(data_addr, buf, words);
  196. if ((io_32bit & 2) && !mmio)
  197. local_irq_restore(flags);
  198. if (((len + 1) & 3) < 2)
  199. return;
  200. buf += len & ~3;
  201. words = 1;
  202. }
  203. if (mmio)
  204. __ide_mm_insw((void __iomem *)data_addr, buf, words);
  205. else
  206. insw(data_addr, buf, words);
  207. }
  208. EXPORT_SYMBOL_GPL(ide_input_data);
  209. /*
  210. * This is used for most PIO data transfers *to* the IDE interface
  211. */
  212. void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  213. unsigned int len)
  214. {
  215. ide_hwif_t *hwif = drive->hwif;
  216. struct ide_io_ports *io_ports = &hwif->io_ports;
  217. unsigned long data_addr = io_ports->data_addr;
  218. unsigned int words = (len + 1) >> 1;
  219. u8 io_32bit = drive->io_32bit;
  220. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  221. if (io_32bit) {
  222. unsigned long uninitialized_var(flags);
  223. if ((io_32bit & 2) && !mmio) {
  224. local_irq_save(flags);
  225. ata_vlb_sync(io_ports->nsect_addr);
  226. }
  227. words >>= 1;
  228. if (mmio)
  229. __ide_mm_outsl((void __iomem *)data_addr, buf, words);
  230. else
  231. outsl(data_addr, buf, words);
  232. if ((io_32bit & 2) && !mmio)
  233. local_irq_restore(flags);
  234. if (((len + 1) & 3) < 2)
  235. return;
  236. buf += len & ~3;
  237. words = 1;
  238. }
  239. if (mmio)
  240. __ide_mm_outsw((void __iomem *)data_addr, buf, words);
  241. else
  242. outsw(data_addr, buf, words);
  243. }
  244. EXPORT_SYMBOL_GPL(ide_output_data);
  245. const struct ide_tp_ops default_tp_ops = {
  246. .exec_command = ide_exec_command,
  247. .read_status = ide_read_status,
  248. .read_altstatus = ide_read_altstatus,
  249. .write_devctl = ide_write_devctl,
  250. .dev_select = ide_dev_select,
  251. .tf_load = ide_tf_load,
  252. .tf_read = ide_tf_read,
  253. .input_data = ide_input_data,
  254. .output_data = ide_output_data,
  255. };