ide-io-std.c 7.7 KB

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