ide-io-std.c 7.6 KB

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