ide-io-std.c 7.0 KB

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