libata-sff.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*
  2. * libata-bmdma.c - helper library for PCI IDE BMDMA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2006 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/pci.h>
  36. #include <linux/libata.h>
  37. #include "libata.h"
  38. /**
  39. * ata_irq_on - Enable interrupts on a port.
  40. * @ap: Port on which interrupts are enabled.
  41. *
  42. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  43. * wait for idle, clear any pending interrupts.
  44. *
  45. * LOCKING:
  46. * Inherited from caller.
  47. */
  48. u8 ata_irq_on(struct ata_port *ap)
  49. {
  50. struct ata_ioports *ioaddr = &ap->ioaddr;
  51. u8 tmp;
  52. ap->ctl &= ~ATA_NIEN;
  53. ap->last_ctl = ap->ctl;
  54. if (ap->flags & ATA_FLAG_MMIO)
  55. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  56. else
  57. outb(ap->ctl, ioaddr->ctl_addr);
  58. tmp = ata_wait_idle(ap);
  59. ap->ops->irq_clear(ap);
  60. return tmp;
  61. }
  62. /**
  63. * ata_tf_load_pio - send taskfile registers to host controller
  64. * @ap: Port to which output is sent
  65. * @tf: ATA taskfile register set
  66. *
  67. * Outputs ATA taskfile to standard ATA host controller.
  68. *
  69. * LOCKING:
  70. * Inherited from caller.
  71. */
  72. static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  73. {
  74. struct ata_ioports *ioaddr = &ap->ioaddr;
  75. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  76. if (tf->ctl != ap->last_ctl) {
  77. outb(tf->ctl, ioaddr->ctl_addr);
  78. ap->last_ctl = tf->ctl;
  79. ata_wait_idle(ap);
  80. }
  81. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  82. outb(tf->hob_feature, ioaddr->feature_addr);
  83. outb(tf->hob_nsect, ioaddr->nsect_addr);
  84. outb(tf->hob_lbal, ioaddr->lbal_addr);
  85. outb(tf->hob_lbam, ioaddr->lbam_addr);
  86. outb(tf->hob_lbah, ioaddr->lbah_addr);
  87. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  88. tf->hob_feature,
  89. tf->hob_nsect,
  90. tf->hob_lbal,
  91. tf->hob_lbam,
  92. tf->hob_lbah);
  93. }
  94. if (is_addr) {
  95. outb(tf->feature, ioaddr->feature_addr);
  96. outb(tf->nsect, ioaddr->nsect_addr);
  97. outb(tf->lbal, ioaddr->lbal_addr);
  98. outb(tf->lbam, ioaddr->lbam_addr);
  99. outb(tf->lbah, ioaddr->lbah_addr);
  100. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  101. tf->feature,
  102. tf->nsect,
  103. tf->lbal,
  104. tf->lbam,
  105. tf->lbah);
  106. }
  107. if (tf->flags & ATA_TFLAG_DEVICE) {
  108. outb(tf->device, ioaddr->device_addr);
  109. VPRINTK("device 0x%X\n", tf->device);
  110. }
  111. ata_wait_idle(ap);
  112. }
  113. /**
  114. * ata_tf_load_mmio - send taskfile registers to host controller
  115. * @ap: Port to which output is sent
  116. * @tf: ATA taskfile register set
  117. *
  118. * Outputs ATA taskfile to standard ATA host controller using MMIO.
  119. *
  120. * LOCKING:
  121. * Inherited from caller.
  122. */
  123. static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  124. {
  125. struct ata_ioports *ioaddr = &ap->ioaddr;
  126. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  127. if (tf->ctl != ap->last_ctl) {
  128. writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  129. ap->last_ctl = tf->ctl;
  130. ata_wait_idle(ap);
  131. }
  132. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  133. writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
  134. writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
  135. writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
  136. writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
  137. writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
  138. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  139. tf->hob_feature,
  140. tf->hob_nsect,
  141. tf->hob_lbal,
  142. tf->hob_lbam,
  143. tf->hob_lbah);
  144. }
  145. if (is_addr) {
  146. writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
  147. writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
  148. writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
  149. writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
  150. writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
  151. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  152. tf->feature,
  153. tf->nsect,
  154. tf->lbal,
  155. tf->lbam,
  156. tf->lbah);
  157. }
  158. if (tf->flags & ATA_TFLAG_DEVICE) {
  159. writeb(tf->device, (void __iomem *) ioaddr->device_addr);
  160. VPRINTK("device 0x%X\n", tf->device);
  161. }
  162. ata_wait_idle(ap);
  163. }
  164. /**
  165. * ata_tf_load - send taskfile registers to host controller
  166. * @ap: Port to which output is sent
  167. * @tf: ATA taskfile register set
  168. *
  169. * Outputs ATA taskfile to standard ATA host controller using MMIO
  170. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  171. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  172. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  173. * hob_lbal, hob_lbam, and hob_lbah.
  174. *
  175. * This function waits for idle (!BUSY and !DRQ) after writing
  176. * registers. If the control register has a new value, this
  177. * function also waits for idle after writing control and before
  178. * writing the remaining registers.
  179. *
  180. * May be used as the tf_load() entry in ata_port_operations.
  181. *
  182. * LOCKING:
  183. * Inherited from caller.
  184. */
  185. void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  186. {
  187. if (ap->flags & ATA_FLAG_MMIO)
  188. ata_tf_load_mmio(ap, tf);
  189. else
  190. ata_tf_load_pio(ap, tf);
  191. }
  192. /**
  193. * ata_exec_command_pio - issue ATA command to host controller
  194. * @ap: port to which command is being issued
  195. * @tf: ATA taskfile register set
  196. *
  197. * Issues PIO write to ATA command register, with proper
  198. * synchronization with interrupt handler / other threads.
  199. *
  200. * LOCKING:
  201. * spin_lock_irqsave(host lock)
  202. */
  203. static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  204. {
  205. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  206. outb(tf->command, ap->ioaddr.command_addr);
  207. ata_pause(ap);
  208. }
  209. /**
  210. * ata_exec_command_mmio - issue ATA command to host controller
  211. * @ap: port to which command is being issued
  212. * @tf: ATA taskfile register set
  213. *
  214. * Issues MMIO write to ATA command register, with proper
  215. * synchronization with interrupt handler / other threads.
  216. *
  217. * FIXME: missing write posting for 400nS delay enforcement
  218. *
  219. * LOCKING:
  220. * spin_lock_irqsave(host lock)
  221. */
  222. static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  223. {
  224. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  225. writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
  226. ata_pause(ap);
  227. }
  228. /**
  229. * ata_exec_command - issue ATA command to host controller
  230. * @ap: port to which command is being issued
  231. * @tf: ATA taskfile register set
  232. *
  233. * Issues PIO/MMIO write to ATA command register, with proper
  234. * synchronization with interrupt handler / other threads.
  235. *
  236. * LOCKING:
  237. * spin_lock_irqsave(host lock)
  238. */
  239. void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  240. {
  241. if (ap->flags & ATA_FLAG_MMIO)
  242. ata_exec_command_mmio(ap, tf);
  243. else
  244. ata_exec_command_pio(ap, tf);
  245. }
  246. /**
  247. * ata_tf_read_pio - input device's ATA taskfile shadow registers
  248. * @ap: Port from which input is read
  249. * @tf: ATA taskfile register set for storing input
  250. *
  251. * Reads ATA taskfile registers for currently-selected device
  252. * into @tf.
  253. *
  254. * LOCKING:
  255. * Inherited from caller.
  256. */
  257. static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
  258. {
  259. struct ata_ioports *ioaddr = &ap->ioaddr;
  260. tf->command = ata_check_status(ap);
  261. tf->feature = inb(ioaddr->error_addr);
  262. tf->nsect = inb(ioaddr->nsect_addr);
  263. tf->lbal = inb(ioaddr->lbal_addr);
  264. tf->lbam = inb(ioaddr->lbam_addr);
  265. tf->lbah = inb(ioaddr->lbah_addr);
  266. tf->device = inb(ioaddr->device_addr);
  267. if (tf->flags & ATA_TFLAG_LBA48) {
  268. outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  269. tf->hob_feature = inb(ioaddr->error_addr);
  270. tf->hob_nsect = inb(ioaddr->nsect_addr);
  271. tf->hob_lbal = inb(ioaddr->lbal_addr);
  272. tf->hob_lbam = inb(ioaddr->lbam_addr);
  273. tf->hob_lbah = inb(ioaddr->lbah_addr);
  274. }
  275. }
  276. /**
  277. * ata_tf_read_mmio - input device's ATA taskfile shadow registers
  278. * @ap: Port from which input is read
  279. * @tf: ATA taskfile register set for storing input
  280. *
  281. * Reads ATA taskfile registers for currently-selected device
  282. * into @tf via MMIO.
  283. *
  284. * LOCKING:
  285. * Inherited from caller.
  286. */
  287. static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
  288. {
  289. struct ata_ioports *ioaddr = &ap->ioaddr;
  290. tf->command = ata_check_status(ap);
  291. tf->feature = readb((void __iomem *)ioaddr->error_addr);
  292. tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
  293. tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
  294. tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
  295. tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
  296. tf->device = readb((void __iomem *)ioaddr->device_addr);
  297. if (tf->flags & ATA_TFLAG_LBA48) {
  298. writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
  299. tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
  300. tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
  301. tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
  302. tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
  303. tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
  304. }
  305. }
  306. /**
  307. * ata_tf_read - input device's ATA taskfile shadow registers
  308. * @ap: Port from which input is read
  309. * @tf: ATA taskfile register set for storing input
  310. *
  311. * Reads ATA taskfile registers for currently-selected device
  312. * into @tf.
  313. *
  314. * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
  315. * is set, also reads the hob registers.
  316. *
  317. * May be used as the tf_read() entry in ata_port_operations.
  318. *
  319. * LOCKING:
  320. * Inherited from caller.
  321. */
  322. void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  323. {
  324. if (ap->flags & ATA_FLAG_MMIO)
  325. ata_tf_read_mmio(ap, tf);
  326. else
  327. ata_tf_read_pio(ap, tf);
  328. }
  329. /**
  330. * ata_check_status_pio - Read device status reg & clear interrupt
  331. * @ap: port where the device is
  332. *
  333. * Reads ATA taskfile status register for currently-selected device
  334. * and return its value. This also clears pending interrupts
  335. * from this device
  336. *
  337. * LOCKING:
  338. * Inherited from caller.
  339. */
  340. static u8 ata_check_status_pio(struct ata_port *ap)
  341. {
  342. return inb(ap->ioaddr.status_addr);
  343. }
  344. /**
  345. * ata_check_status_mmio - Read device status reg & clear interrupt
  346. * @ap: port where the device is
  347. *
  348. * Reads ATA taskfile status register for currently-selected device
  349. * via MMIO and return its value. This also clears pending interrupts
  350. * from this device
  351. *
  352. * LOCKING:
  353. * Inherited from caller.
  354. */
  355. static u8 ata_check_status_mmio(struct ata_port *ap)
  356. {
  357. return readb((void __iomem *) ap->ioaddr.status_addr);
  358. }
  359. /**
  360. * ata_check_status - Read device status reg & clear interrupt
  361. * @ap: port where the device is
  362. *
  363. * Reads ATA taskfile status register for currently-selected device
  364. * and return its value. This also clears pending interrupts
  365. * from this device
  366. *
  367. * May be used as the check_status() entry in ata_port_operations.
  368. *
  369. * LOCKING:
  370. * Inherited from caller.
  371. */
  372. u8 ata_check_status(struct ata_port *ap)
  373. {
  374. if (ap->flags & ATA_FLAG_MMIO)
  375. return ata_check_status_mmio(ap);
  376. return ata_check_status_pio(ap);
  377. }
  378. /**
  379. * ata_altstatus - Read device alternate status reg
  380. * @ap: port where the device is
  381. *
  382. * Reads ATA taskfile alternate status register for
  383. * currently-selected device and return its value.
  384. *
  385. * Note: may NOT be used as the check_altstatus() entry in
  386. * ata_port_operations.
  387. *
  388. * LOCKING:
  389. * Inherited from caller.
  390. */
  391. u8 ata_altstatus(struct ata_port *ap)
  392. {
  393. if (ap->ops->check_altstatus)
  394. return ap->ops->check_altstatus(ap);
  395. if (ap->flags & ATA_FLAG_MMIO)
  396. return readb((void __iomem *)ap->ioaddr.altstatus_addr);
  397. return inb(ap->ioaddr.altstatus_addr);
  398. }
  399. /**
  400. * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
  401. * @qc: Info associated with this ATA transaction.
  402. *
  403. * LOCKING:
  404. * spin_lock_irqsave(host lock)
  405. */
  406. static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
  407. {
  408. struct ata_port *ap = qc->ap;
  409. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  410. u8 dmactl;
  411. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  412. /* load PRD table addr. */
  413. mb(); /* make sure PRD table writes are visible to controller */
  414. writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
  415. /* specify data direction, triple-check start bit is clear */
  416. dmactl = readb(mmio + ATA_DMA_CMD);
  417. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  418. if (!rw)
  419. dmactl |= ATA_DMA_WR;
  420. writeb(dmactl, mmio + ATA_DMA_CMD);
  421. /* issue r/w command */
  422. ap->ops->exec_command(ap, &qc->tf);
  423. }
  424. /**
  425. * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
  426. * @qc: Info associated with this ATA transaction.
  427. *
  428. * LOCKING:
  429. * spin_lock_irqsave(host lock)
  430. */
  431. static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
  432. {
  433. struct ata_port *ap = qc->ap;
  434. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  435. u8 dmactl;
  436. /* start host DMA transaction */
  437. dmactl = readb(mmio + ATA_DMA_CMD);
  438. writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
  439. /* Strictly, one may wish to issue a readb() here, to
  440. * flush the mmio write. However, control also passes
  441. * to the hardware at this point, and it will interrupt
  442. * us when we are to resume control. So, in effect,
  443. * we don't care when the mmio write flushes.
  444. * Further, a read of the DMA status register _immediately_
  445. * following the write may not be what certain flaky hardware
  446. * is expected, so I think it is best to not add a readb()
  447. * without first all the MMIO ATA cards/mobos.
  448. * Or maybe I'm just being paranoid.
  449. */
  450. }
  451. /**
  452. * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
  453. * @qc: Info associated with this ATA transaction.
  454. *
  455. * LOCKING:
  456. * spin_lock_irqsave(host lock)
  457. */
  458. static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
  459. {
  460. struct ata_port *ap = qc->ap;
  461. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  462. u8 dmactl;
  463. /* load PRD table addr. */
  464. outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  465. /* specify data direction, triple-check start bit is clear */
  466. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  467. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  468. if (!rw)
  469. dmactl |= ATA_DMA_WR;
  470. outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  471. /* issue r/w command */
  472. ap->ops->exec_command(ap, &qc->tf);
  473. }
  474. /**
  475. * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
  476. * @qc: Info associated with this ATA transaction.
  477. *
  478. * LOCKING:
  479. * spin_lock_irqsave(host lock)
  480. */
  481. static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
  482. {
  483. struct ata_port *ap = qc->ap;
  484. u8 dmactl;
  485. /* start host DMA transaction */
  486. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  487. outb(dmactl | ATA_DMA_START,
  488. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  489. }
  490. /**
  491. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  492. * @qc: Info associated with this ATA transaction.
  493. *
  494. * Writes the ATA_DMA_START flag to the DMA command register.
  495. *
  496. * May be used as the bmdma_start() entry in ata_port_operations.
  497. *
  498. * LOCKING:
  499. * spin_lock_irqsave(host lock)
  500. */
  501. void ata_bmdma_start(struct ata_queued_cmd *qc)
  502. {
  503. if (qc->ap->flags & ATA_FLAG_MMIO)
  504. ata_bmdma_start_mmio(qc);
  505. else
  506. ata_bmdma_start_pio(qc);
  507. }
  508. /**
  509. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  510. * @qc: Info associated with this ATA transaction.
  511. *
  512. * Writes address of PRD table to device's PRD Table Address
  513. * register, sets the DMA control register, and calls
  514. * ops->exec_command() to start the transfer.
  515. *
  516. * May be used as the bmdma_setup() entry in ata_port_operations.
  517. *
  518. * LOCKING:
  519. * spin_lock_irqsave(host lock)
  520. */
  521. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  522. {
  523. if (qc->ap->flags & ATA_FLAG_MMIO)
  524. ata_bmdma_setup_mmio(qc);
  525. else
  526. ata_bmdma_setup_pio(qc);
  527. }
  528. /**
  529. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  530. * @ap: Port associated with this ATA transaction.
  531. *
  532. * Clear interrupt and error flags in DMA status register.
  533. *
  534. * May be used as the irq_clear() entry in ata_port_operations.
  535. *
  536. * LOCKING:
  537. * spin_lock_irqsave(host lock)
  538. */
  539. void ata_bmdma_irq_clear(struct ata_port *ap)
  540. {
  541. if (!ap->ioaddr.bmdma_addr)
  542. return;
  543. if (ap->flags & ATA_FLAG_MMIO) {
  544. void __iomem *mmio =
  545. ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
  546. writeb(readb(mmio), mmio);
  547. } else {
  548. unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
  549. outb(inb(addr), addr);
  550. }
  551. }
  552. /**
  553. * ata_bmdma_status - Read PCI IDE BMDMA status
  554. * @ap: Port associated with this ATA transaction.
  555. *
  556. * Read and return BMDMA status register.
  557. *
  558. * May be used as the bmdma_status() entry in ata_port_operations.
  559. *
  560. * LOCKING:
  561. * spin_lock_irqsave(host lock)
  562. */
  563. u8 ata_bmdma_status(struct ata_port *ap)
  564. {
  565. u8 host_stat;
  566. if (ap->flags & ATA_FLAG_MMIO) {
  567. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  568. host_stat = readb(mmio + ATA_DMA_STATUS);
  569. } else
  570. host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  571. return host_stat;
  572. }
  573. /**
  574. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  575. * @qc: Command we are ending DMA for
  576. *
  577. * Clears the ATA_DMA_START flag in the dma control register
  578. *
  579. * May be used as the bmdma_stop() entry in ata_port_operations.
  580. *
  581. * LOCKING:
  582. * spin_lock_irqsave(host lock)
  583. */
  584. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  585. {
  586. struct ata_port *ap = qc->ap;
  587. if (ap->flags & ATA_FLAG_MMIO) {
  588. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  589. /* clear start/stop bit */
  590. writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  591. mmio + ATA_DMA_CMD);
  592. } else {
  593. /* clear start/stop bit */
  594. outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
  595. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  596. }
  597. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  598. ata_altstatus(ap); /* dummy read */
  599. }
  600. /**
  601. * ata_bmdma_freeze - Freeze BMDMA controller port
  602. * @ap: port to freeze
  603. *
  604. * Freeze BMDMA controller port.
  605. *
  606. * LOCKING:
  607. * Inherited from caller.
  608. */
  609. void ata_bmdma_freeze(struct ata_port *ap)
  610. {
  611. struct ata_ioports *ioaddr = &ap->ioaddr;
  612. ap->ctl |= ATA_NIEN;
  613. ap->last_ctl = ap->ctl;
  614. if (ap->flags & ATA_FLAG_MMIO)
  615. writeb(ap->ctl, (void __iomem *)ioaddr->ctl_addr);
  616. else
  617. outb(ap->ctl, ioaddr->ctl_addr);
  618. /* Under certain circumstances, some controllers raise IRQ on
  619. * ATA_NIEN manipulation. Also, many controllers fail to mask
  620. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  621. */
  622. ata_chk_status(ap);
  623. ap->ops->irq_clear(ap);
  624. }
  625. /**
  626. * ata_bmdma_thaw - Thaw BMDMA controller port
  627. * @ap: port to thaw
  628. *
  629. * Thaw BMDMA controller port.
  630. *
  631. * LOCKING:
  632. * Inherited from caller.
  633. */
  634. void ata_bmdma_thaw(struct ata_port *ap)
  635. {
  636. /* clear & re-enable interrupts */
  637. ata_chk_status(ap);
  638. ap->ops->irq_clear(ap);
  639. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  640. ata_irq_on(ap);
  641. }
  642. /**
  643. * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
  644. * @ap: port to handle error for
  645. * @prereset: prereset method (can be NULL)
  646. * @softreset: softreset method (can be NULL)
  647. * @hardreset: hardreset method (can be NULL)
  648. * @postreset: postreset method (can be NULL)
  649. *
  650. * Handle error for ATA BMDMA controller. It can handle both
  651. * PATA and SATA controllers. Many controllers should be able to
  652. * use this EH as-is or with some added handling before and
  653. * after.
  654. *
  655. * This function is intended to be used for constructing
  656. * ->error_handler callback by low level drivers.
  657. *
  658. * LOCKING:
  659. * Kernel thread context (may sleep)
  660. */
  661. void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  662. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  663. ata_postreset_fn_t postreset)
  664. {
  665. struct ata_queued_cmd *qc;
  666. unsigned long flags;
  667. int thaw = 0;
  668. qc = __ata_qc_from_tag(ap, ap->active_tag);
  669. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  670. qc = NULL;
  671. /* reset PIO HSM and stop DMA engine */
  672. spin_lock_irqsave(ap->lock, flags);
  673. ap->hsm_task_state = HSM_ST_IDLE;
  674. if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
  675. qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
  676. u8 host_stat;
  677. host_stat = ap->ops->bmdma_status(ap);
  678. /* BMDMA controllers indicate host bus error by
  679. * setting DMA_ERR bit and timing out. As it wasn't
  680. * really a timeout event, adjust error mask and
  681. * cancel frozen state.
  682. */
  683. if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
  684. qc->err_mask = AC_ERR_HOST_BUS;
  685. thaw = 1;
  686. }
  687. ap->ops->bmdma_stop(qc);
  688. }
  689. ata_altstatus(ap);
  690. ata_chk_status(ap);
  691. ap->ops->irq_clear(ap);
  692. spin_unlock_irqrestore(ap->lock, flags);
  693. if (thaw)
  694. ata_eh_thaw_port(ap);
  695. /* PIO and DMA engines have been stopped, perform recovery */
  696. ata_do_eh(ap, prereset, softreset, hardreset, postreset);
  697. }
  698. /**
  699. * ata_bmdma_error_handler - Stock error handler for BMDMA controller
  700. * @ap: port to handle error for
  701. *
  702. * Stock error handler for BMDMA controller.
  703. *
  704. * LOCKING:
  705. * Kernel thread context (may sleep)
  706. */
  707. void ata_bmdma_error_handler(struct ata_port *ap)
  708. {
  709. ata_reset_fn_t hardreset;
  710. hardreset = NULL;
  711. if (sata_scr_valid(ap))
  712. hardreset = sata_std_hardreset;
  713. ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  714. ata_std_postreset);
  715. }
  716. /**
  717. * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
  718. * BMDMA controller
  719. * @qc: internal command to clean up
  720. *
  721. * LOCKING:
  722. * Kernel thread context (may sleep)
  723. */
  724. void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
  725. {
  726. if (qc->ap->ioaddr.bmdma_addr)
  727. ata_bmdma_stop(qc);
  728. }
  729. #ifdef CONFIG_PCI
  730. /**
  731. * ata_pci_init_native_mode - Initialize native-mode driver
  732. * @pdev: pci device to be initialized
  733. * @port: array[2] of pointers to port info structures.
  734. * @ports: bitmap of ports present
  735. *
  736. * Utility function which allocates and initializes an
  737. * ata_probe_ent structure for a standard dual-port
  738. * PIO-based IDE controller. The returned ata_probe_ent
  739. * structure can be passed to ata_device_add(). The returned
  740. * ata_probe_ent structure should then be freed with kfree().
  741. *
  742. * The caller need only pass the address of the primary port, the
  743. * secondary will be deduced automatically. If the device has non
  744. * standard secondary port mappings this function can be called twice,
  745. * once for each interface.
  746. */
  747. struct ata_probe_ent *
  748. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  749. {
  750. struct ata_probe_ent *probe_ent =
  751. ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  752. int p = 0;
  753. unsigned long bmdma;
  754. if (!probe_ent)
  755. return NULL;
  756. probe_ent->irq = pdev->irq;
  757. probe_ent->irq_flags = IRQF_SHARED;
  758. if (ports & ATA_PORT_PRIMARY) {
  759. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
  760. probe_ent->port[p].altstatus_addr =
  761. probe_ent->port[p].ctl_addr =
  762. pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
  763. bmdma = pci_resource_start(pdev, 4);
  764. if (bmdma) {
  765. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  766. (inb(bmdma + 2) & 0x80))
  767. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  768. probe_ent->port[p].bmdma_addr = bmdma;
  769. }
  770. ata_std_ports(&probe_ent->port[p]);
  771. p++;
  772. }
  773. if (ports & ATA_PORT_SECONDARY) {
  774. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
  775. probe_ent->port[p].altstatus_addr =
  776. probe_ent->port[p].ctl_addr =
  777. pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
  778. bmdma = pci_resource_start(pdev, 4);
  779. if (bmdma) {
  780. bmdma += 8;
  781. if ((!(port[p]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  782. (inb(bmdma + 2) & 0x80))
  783. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  784. probe_ent->port[p].bmdma_addr = bmdma;
  785. }
  786. ata_std_ports(&probe_ent->port[p]);
  787. probe_ent->pinfo2 = port[1];
  788. p++;
  789. }
  790. probe_ent->n_ports = p;
  791. return probe_ent;
  792. }
  793. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
  794. struct ata_port_info **port, int port_mask)
  795. {
  796. struct ata_probe_ent *probe_ent;
  797. unsigned long bmdma = pci_resource_start(pdev, 4);
  798. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  799. if (!probe_ent)
  800. return NULL;
  801. probe_ent->n_ports = 2;
  802. probe_ent->irq_flags = IRQF_SHARED;
  803. if (port_mask & ATA_PORT_PRIMARY) {
  804. probe_ent->irq = ATA_PRIMARY_IRQ;
  805. probe_ent->port[0].cmd_addr = ATA_PRIMARY_CMD;
  806. probe_ent->port[0].altstatus_addr =
  807. probe_ent->port[0].ctl_addr = ATA_PRIMARY_CTL;
  808. if (bmdma) {
  809. probe_ent->port[0].bmdma_addr = bmdma;
  810. if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  811. (inb(bmdma + 2) & 0x80))
  812. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  813. }
  814. ata_std_ports(&probe_ent->port[0]);
  815. } else
  816. probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
  817. if (port_mask & ATA_PORT_SECONDARY) {
  818. if (probe_ent->irq)
  819. probe_ent->irq2 = ATA_SECONDARY_IRQ;
  820. else
  821. probe_ent->irq = ATA_SECONDARY_IRQ;
  822. probe_ent->port[1].cmd_addr = ATA_SECONDARY_CMD;
  823. probe_ent->port[1].altstatus_addr =
  824. probe_ent->port[1].ctl_addr = ATA_SECONDARY_CTL;
  825. if (bmdma) {
  826. probe_ent->port[1].bmdma_addr = bmdma + 8;
  827. if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  828. (inb(bmdma + 10) & 0x80))
  829. probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
  830. }
  831. ata_std_ports(&probe_ent->port[1]);
  832. /* FIXME: could be pointing to stack area; must copy */
  833. probe_ent->pinfo2 = port[1];
  834. } else
  835. probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
  836. return probe_ent;
  837. }
  838. /**
  839. * ata_pci_init_one - Initialize/register PCI IDE host controller
  840. * @pdev: Controller to be initialized
  841. * @port_info: Information from low-level host driver
  842. * @n_ports: Number of ports attached to host controller
  843. *
  844. * This is a helper function which can be called from a driver's
  845. * xxx_init_one() probe function if the hardware uses traditional
  846. * IDE taskfile registers.
  847. *
  848. * This function calls pci_enable_device(), reserves its register
  849. * regions, sets the dma mask, enables bus master mode, and calls
  850. * ata_device_add()
  851. *
  852. * ASSUMPTION:
  853. * Nobody makes a single channel controller that appears solely as
  854. * the secondary legacy port on PCI.
  855. *
  856. * LOCKING:
  857. * Inherited from PCI layer (may sleep).
  858. *
  859. * RETURNS:
  860. * Zero on success, negative on errno-based value on error.
  861. */
  862. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  863. unsigned int n_ports)
  864. {
  865. struct ata_probe_ent *probe_ent = NULL;
  866. struct ata_port_info *port[2];
  867. u8 mask;
  868. unsigned int legacy_mode = 0;
  869. int disable_dev_on_err = 1;
  870. int rc;
  871. DPRINTK("ENTER\n");
  872. BUG_ON(n_ports < 1 || n_ports > 2);
  873. port[0] = port_info[0];
  874. if (n_ports > 1)
  875. port[1] = port_info[1];
  876. else
  877. port[1] = port[0];
  878. /* FIXME: Really for ATA it isn't safe because the device may be
  879. multi-purpose and we want to leave it alone if it was already
  880. enabled. Secondly for shared use as Arjan says we want refcounting
  881. Checking dev->is_enabled is insufficient as this is not set at
  882. boot for the primary video which is BIOS enabled
  883. */
  884. rc = pci_enable_device(pdev);
  885. if (rc)
  886. return rc;
  887. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  888. u8 tmp8;
  889. /* TODO: What if one channel is in native mode ... */
  890. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  891. mask = (1 << 2) | (1 << 0);
  892. if ((tmp8 & mask) != mask)
  893. legacy_mode = (1 << 3);
  894. #if defined(CONFIG_NO_ATA_LEGACY)
  895. /* Some platforms with PCI limits cannot address compat
  896. port space. In that case we punt if their firmware has
  897. left a device in compatibility mode */
  898. if (legacy_mode) {
  899. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  900. return -EOPNOTSUPP;
  901. }
  902. #endif
  903. }
  904. if (!legacy_mode) {
  905. rc = pci_request_regions(pdev, DRV_NAME);
  906. if (rc) {
  907. disable_dev_on_err = 0;
  908. goto err_out;
  909. }
  910. } else {
  911. /* Deal with combined mode hack. This side of the logic all
  912. goes away once the combined mode hack is killed in 2.6.21 */
  913. if (!request_region(ATA_PRIMARY_CMD, 8, "libata")) {
  914. struct resource *conflict, res;
  915. res.start = ATA_PRIMARY_CMD;
  916. res.end = ATA_PRIMARY_CMD + 8 - 1;
  917. conflict = ____request_resource(&ioport_resource, &res);
  918. while (conflict->child)
  919. conflict = ____request_resource(conflict, &res);
  920. if (!strcmp(conflict->name, "libata"))
  921. legacy_mode |= ATA_PORT_PRIMARY;
  922. else {
  923. disable_dev_on_err = 0;
  924. printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
  925. "ata: conflict with %s\n",
  926. ATA_PRIMARY_CMD,
  927. conflict->name);
  928. }
  929. } else
  930. legacy_mode |= ATA_PORT_PRIMARY;
  931. if (!request_region(ATA_SECONDARY_CMD, 8, "libata")) {
  932. struct resource *conflict, res;
  933. res.start = ATA_SECONDARY_CMD;
  934. res.end = ATA_SECONDARY_CMD + 8 - 1;
  935. conflict = ____request_resource(&ioport_resource, &res);
  936. while (conflict->child)
  937. conflict = ____request_resource(conflict, &res);
  938. if (!strcmp(conflict->name, "libata"))
  939. legacy_mode |= ATA_PORT_SECONDARY;
  940. else {
  941. disable_dev_on_err = 0;
  942. printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
  943. "ata: conflict with %s\n",
  944. ATA_SECONDARY_CMD,
  945. conflict->name);
  946. }
  947. } else
  948. legacy_mode |= ATA_PORT_SECONDARY;
  949. if (legacy_mode & ATA_PORT_PRIMARY)
  950. pci_request_region(pdev, 1, DRV_NAME);
  951. if (legacy_mode & ATA_PORT_SECONDARY)
  952. pci_request_region(pdev, 3, DRV_NAME);
  953. /* If there is a DMA resource, allocate it */
  954. pci_request_region(pdev, 4, DRV_NAME);
  955. }
  956. /* we have legacy mode, but all ports are unavailable */
  957. if (legacy_mode == (1 << 3)) {
  958. rc = -EBUSY;
  959. goto err_out_regions;
  960. }
  961. /* TODO: If we get no DMA mask we should fall back to PIO */
  962. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  963. if (rc)
  964. goto err_out_regions;
  965. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  966. if (rc)
  967. goto err_out_regions;
  968. if (legacy_mode) {
  969. probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
  970. } else {
  971. if (n_ports == 2)
  972. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  973. else
  974. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  975. }
  976. if (!probe_ent) {
  977. rc = -ENOMEM;
  978. goto err_out_regions;
  979. }
  980. pci_set_master(pdev);
  981. if (!ata_device_add(probe_ent)) {
  982. rc = -ENODEV;
  983. goto err_out_ent;
  984. }
  985. kfree(probe_ent);
  986. return 0;
  987. err_out_ent:
  988. kfree(probe_ent);
  989. err_out_regions:
  990. /* All this conditional stuff is needed for the combined mode hack
  991. until 2.6.21 when it can go */
  992. if (legacy_mode) {
  993. pci_release_region(pdev, 4);
  994. if (legacy_mode & ATA_PORT_PRIMARY) {
  995. release_region(ATA_PRIMARY_CMD, 8);
  996. pci_release_region(pdev, 1);
  997. }
  998. if (legacy_mode & ATA_PORT_SECONDARY) {
  999. release_region(ATA_SECONDARY_CMD, 8);
  1000. pci_release_region(pdev, 3);
  1001. }
  1002. } else
  1003. pci_release_regions(pdev);
  1004. err_out:
  1005. if (disable_dev_on_err)
  1006. pci_disable_device(pdev);
  1007. return rc;
  1008. }
  1009. /**
  1010. * ata_pci_clear_simplex - attempt to kick device out of simplex
  1011. * @pdev: PCI device
  1012. *
  1013. * Some PCI ATA devices report simplex mode but in fact can be told to
  1014. * enter non simplex mode. This implements the neccessary logic to
  1015. * perform the task on such devices. Calling it on other devices will
  1016. * have -undefined- behaviour.
  1017. */
  1018. int ata_pci_clear_simplex(struct pci_dev *pdev)
  1019. {
  1020. unsigned long bmdma = pci_resource_start(pdev, 4);
  1021. u8 simplex;
  1022. if (bmdma == 0)
  1023. return -ENOENT;
  1024. simplex = inb(bmdma + 0x02);
  1025. outb(simplex & 0x60, bmdma + 0x02);
  1026. simplex = inb(bmdma + 0x02);
  1027. if (simplex & 0x80)
  1028. return -EOPNOTSUPP;
  1029. return 0;
  1030. }
  1031. unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
  1032. {
  1033. /* Filter out DMA modes if the device has been configured by
  1034. the BIOS as PIO only */
  1035. if (ap->ioaddr.bmdma_addr == 0)
  1036. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1037. return xfer_mask;
  1038. }
  1039. #endif /* CONFIG_PCI */