ide-io-std.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/kernel.h>
  3. #include <linux/export.h>
  4. #include <linux/ide.h>
  5. #if defined(CONFIG_ARM) || defined(CONFIG_M68K) || defined(CONFIG_MIPS) || \
  6. defined(CONFIG_PARISC) || defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  7. #include <asm/ide.h>
  8. #else
  9. #include <asm-generic/ide_iops.h>
  10. #endif
  11. /*
  12. * Conventional PIO operations for ATA devices
  13. */
  14. static u8 ide_inb(unsigned long port)
  15. {
  16. return (u8) inb(port);
  17. }
  18. static void ide_outb(u8 val, unsigned long port)
  19. {
  20. outb(val, port);
  21. }
  22. /*
  23. * MMIO operations, typically used for SATA controllers
  24. */
  25. static u8 ide_mm_inb(unsigned long port)
  26. {
  27. return (u8) readb((void __iomem *) port);
  28. }
  29. static void ide_mm_outb(u8 value, unsigned long port)
  30. {
  31. writeb(value, (void __iomem *) port);
  32. }
  33. void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
  34. {
  35. if (hwif->host_flags & IDE_HFLAG_MMIO)
  36. writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
  37. else
  38. outb(cmd, hwif->io_ports.command_addr);
  39. }
  40. EXPORT_SYMBOL_GPL(ide_exec_command);
  41. u8 ide_read_status(ide_hwif_t *hwif)
  42. {
  43. if (hwif->host_flags & IDE_HFLAG_MMIO)
  44. return readb((void __iomem *)hwif->io_ports.status_addr);
  45. else
  46. return inb(hwif->io_ports.status_addr);
  47. }
  48. EXPORT_SYMBOL_GPL(ide_read_status);
  49. u8 ide_read_altstatus(ide_hwif_t *hwif)
  50. {
  51. if (hwif->host_flags & IDE_HFLAG_MMIO)
  52. return readb((void __iomem *)hwif->io_ports.ctl_addr);
  53. else
  54. return inb(hwif->io_ports.ctl_addr);
  55. }
  56. EXPORT_SYMBOL_GPL(ide_read_altstatus);
  57. void ide_write_devctl(ide_hwif_t *hwif, u8 ctl)
  58. {
  59. if (hwif->host_flags & IDE_HFLAG_MMIO)
  60. writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
  61. else
  62. outb(ctl, hwif->io_ports.ctl_addr);
  63. }
  64. EXPORT_SYMBOL_GPL(ide_write_devctl);
  65. void ide_dev_select(ide_drive_t *drive)
  66. {
  67. ide_hwif_t *hwif = drive->hwif;
  68. u8 select = drive->select | ATA_DEVICE_OBS;
  69. if (hwif->host_flags & IDE_HFLAG_MMIO)
  70. writeb(select, (void __iomem *)hwif->io_ports.device_addr);
  71. else
  72. outb(select, hwif->io_ports.device_addr);
  73. }
  74. EXPORT_SYMBOL_GPL(ide_dev_select);
  75. void ide_tf_load(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid)
  76. {
  77. ide_hwif_t *hwif = drive->hwif;
  78. struct ide_io_ports *io_ports = &hwif->io_ports;
  79. void (*tf_outb)(u8 addr, unsigned long port);
  80. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  81. if (mmio)
  82. tf_outb = ide_mm_outb;
  83. else
  84. tf_outb = ide_outb;
  85. if (valid & IDE_VALID_FEATURE)
  86. tf_outb(tf->feature, io_ports->feature_addr);
  87. if (valid & IDE_VALID_NSECT)
  88. tf_outb(tf->nsect, io_ports->nsect_addr);
  89. if (valid & IDE_VALID_LBAL)
  90. tf_outb(tf->lbal, io_ports->lbal_addr);
  91. if (valid & IDE_VALID_LBAM)
  92. tf_outb(tf->lbam, io_ports->lbam_addr);
  93. if (valid & IDE_VALID_LBAH)
  94. tf_outb(tf->lbah, io_ports->lbah_addr);
  95. if (valid & IDE_VALID_DEVICE)
  96. tf_outb(tf->device, io_ports->device_addr);
  97. }
  98. EXPORT_SYMBOL_GPL(ide_tf_load);
  99. void ide_tf_read(ide_drive_t *drive, struct ide_taskfile *tf, u8 valid)
  100. {
  101. ide_hwif_t *hwif = drive->hwif;
  102. struct ide_io_ports *io_ports = &hwif->io_ports;
  103. u8 (*tf_inb)(unsigned long port);
  104. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  105. if (mmio)
  106. tf_inb = ide_mm_inb;
  107. else
  108. tf_inb = ide_inb;
  109. if (valid & IDE_VALID_ERROR)
  110. tf->error = tf_inb(io_ports->feature_addr);
  111. if (valid & IDE_VALID_NSECT)
  112. tf->nsect = tf_inb(io_ports->nsect_addr);
  113. if (valid & IDE_VALID_LBAL)
  114. tf->lbal = tf_inb(io_ports->lbal_addr);
  115. if (valid & IDE_VALID_LBAM)
  116. tf->lbam = tf_inb(io_ports->lbam_addr);
  117. if (valid & IDE_VALID_LBAH)
  118. tf->lbah = tf_inb(io_ports->lbah_addr);
  119. if (valid & IDE_VALID_DEVICE)
  120. tf->device = tf_inb(io_ports->device_addr);
  121. }
  122. EXPORT_SYMBOL_GPL(ide_tf_read);
  123. /*
  124. * Some localbus EIDE interfaces require a special access sequence
  125. * when using 32-bit I/O instructions to transfer data. We call this
  126. * the "vlb_sync" sequence, which consists of three successive reads
  127. * of the sector count register location, with interrupts disabled
  128. * to ensure that the reads all happen together.
  129. */
  130. static void ata_vlb_sync(unsigned long port)
  131. {
  132. (void)inb(port);
  133. (void)inb(port);
  134. (void)inb(port);
  135. }
  136. /*
  137. * This is used for most PIO data transfers *from* the IDE interface
  138. *
  139. * These routines will round up any request for an odd number of bytes,
  140. * so if an odd len is specified, be sure that there's at least one
  141. * extra byte allocated for the buffer.
  142. */
  143. void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  144. unsigned int len)
  145. {
  146. ide_hwif_t *hwif = drive->hwif;
  147. struct ide_io_ports *io_ports = &hwif->io_ports;
  148. unsigned long data_addr = io_ports->data_addr;
  149. unsigned int words = (len + 1) >> 1;
  150. u8 io_32bit = drive->io_32bit;
  151. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  152. if (io_32bit) {
  153. unsigned long flags;
  154. if ((io_32bit & 2) && !mmio) {
  155. local_irq_save(flags);
  156. ata_vlb_sync(io_ports->nsect_addr);
  157. }
  158. words >>= 1;
  159. if (mmio)
  160. __ide_mm_insl((void __iomem *)data_addr, buf, words);
  161. else
  162. insl(data_addr, buf, words);
  163. if ((io_32bit & 2) && !mmio)
  164. local_irq_restore(flags);
  165. if (((len + 1) & 3) < 2)
  166. return;
  167. buf += len & ~3;
  168. words = 1;
  169. }
  170. if (mmio)
  171. __ide_mm_insw((void __iomem *)data_addr, buf, words);
  172. else
  173. insw(data_addr, buf, words);
  174. }
  175. EXPORT_SYMBOL_GPL(ide_input_data);
  176. /*
  177. * This is used for most PIO data transfers *to* the IDE interface
  178. */
  179. void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  180. unsigned int len)
  181. {
  182. ide_hwif_t *hwif = drive->hwif;
  183. struct ide_io_ports *io_ports = &hwif->io_ports;
  184. unsigned long data_addr = io_ports->data_addr;
  185. unsigned int words = (len + 1) >> 1;
  186. u8 io_32bit = drive->io_32bit;
  187. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  188. if (io_32bit) {
  189. unsigned long flags;
  190. if ((io_32bit & 2) && !mmio) {
  191. local_irq_save(flags);
  192. ata_vlb_sync(io_ports->nsect_addr);
  193. }
  194. words >>= 1;
  195. if (mmio)
  196. __ide_mm_outsl((void __iomem *)data_addr, buf, words);
  197. else
  198. outsl(data_addr, buf, words);
  199. if ((io_32bit & 2) && !mmio)
  200. local_irq_restore(flags);
  201. if (((len + 1) & 3) < 2)
  202. return;
  203. buf += len & ~3;
  204. words = 1;
  205. }
  206. if (mmio)
  207. __ide_mm_outsw((void __iomem *)data_addr, buf, words);
  208. else
  209. outsw(data_addr, buf, words);
  210. }
  211. EXPORT_SYMBOL_GPL(ide_output_data);
  212. const struct ide_tp_ops default_tp_ops = {
  213. .exec_command = ide_exec_command,
  214. .read_status = ide_read_status,
  215. .read_altstatus = ide_read_altstatus,
  216. .write_devctl = ide_write_devctl,
  217. .dev_select = ide_dev_select,
  218. .tf_load = ide_tf_load,
  219. .tf_read = ide_tf_read,
  220. .input_data = ide_input_data,
  221. .output_data = ide_output_data,
  222. };