sata_svw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * sata_svw.c - ServerWorks / Apple K2 SATA
  3. *
  4. * Maintained by: Benjamin Herrenschmidt <benh@kernel.crashing.org> and
  5. * Jeff Garzik <jgarzik@pobox.com>
  6. * Please ALWAYS copy linux-ide@vger.kernel.org
  7. * on emails.
  8. *
  9. * Copyright 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  10. *
  11. * Bits from Jeff Garzik, Copyright RedHat, Inc.
  12. *
  13. * This driver probably works with non-Apple versions of the
  14. * Broadcom chipset...
  15. *
  16. * The contents of this file are subject to the Open
  17. * Software License version 1.1 that can be found at
  18. * http://www.opensource.org/licenses/osl-1.1.txt and is included herein
  19. * by reference.
  20. *
  21. * Alternatively, the contents of this file may be used under the terms
  22. * of the GNU General Public License version 2 (the "GPL") as distributed
  23. * in the kernel source COPYING file, in which case the provisions of
  24. * the GPL are applicable instead of the above. If you wish to allow
  25. * the use of your version of this file only under the terms of the
  26. * GPL and not to allow others to use your version of this file under
  27. * the OSL, indicate your decision by deleting the provisions above and
  28. * replace them with the notice and other provisions required by the GPL.
  29. * If you do not delete the provisions above, a recipient may use your
  30. * version of this file under either the OSL or the GPL.
  31. *
  32. */
  33. #include <linux/config.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/delay.h>
  40. #include <linux/interrupt.h>
  41. #include "scsi.h"
  42. #include <scsi/scsi_host.h>
  43. #include <linux/libata.h>
  44. #ifdef CONFIG_PPC_OF
  45. #include <asm/prom.h>
  46. #include <asm/pci-bridge.h>
  47. #endif /* CONFIG_PPC_OF */
  48. #define DRV_NAME "sata_svw"
  49. #define DRV_VERSION "1.06"
  50. /* Taskfile registers offsets */
  51. #define K2_SATA_TF_CMD_OFFSET 0x00
  52. #define K2_SATA_TF_DATA_OFFSET 0x00
  53. #define K2_SATA_TF_ERROR_OFFSET 0x04
  54. #define K2_SATA_TF_NSECT_OFFSET 0x08
  55. #define K2_SATA_TF_LBAL_OFFSET 0x0c
  56. #define K2_SATA_TF_LBAM_OFFSET 0x10
  57. #define K2_SATA_TF_LBAH_OFFSET 0x14
  58. #define K2_SATA_TF_DEVICE_OFFSET 0x18
  59. #define K2_SATA_TF_CMDSTAT_OFFSET 0x1c
  60. #define K2_SATA_TF_CTL_OFFSET 0x20
  61. /* DMA base */
  62. #define K2_SATA_DMA_CMD_OFFSET 0x30
  63. /* SCRs base */
  64. #define K2_SATA_SCR_STATUS_OFFSET 0x40
  65. #define K2_SATA_SCR_ERROR_OFFSET 0x44
  66. #define K2_SATA_SCR_CONTROL_OFFSET 0x48
  67. /* Others */
  68. #define K2_SATA_SICR1_OFFSET 0x80
  69. #define K2_SATA_SICR2_OFFSET 0x84
  70. #define K2_SATA_SIM_OFFSET 0x88
  71. /* Port stride */
  72. #define K2_SATA_PORT_OFFSET 0x100
  73. static u32 k2_sata_scr_read (struct ata_port *ap, unsigned int sc_reg)
  74. {
  75. if (sc_reg > SCR_CONTROL)
  76. return 0xffffffffU;
  77. return readl((void *) ap->ioaddr.scr_addr + (sc_reg * 4));
  78. }
  79. static void k2_sata_scr_write (struct ata_port *ap, unsigned int sc_reg,
  80. u32 val)
  81. {
  82. if (sc_reg > SCR_CONTROL)
  83. return;
  84. writel(val, (void *) ap->ioaddr.scr_addr + (sc_reg * 4));
  85. }
  86. static void k2_sata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
  87. {
  88. struct ata_ioports *ioaddr = &ap->ioaddr;
  89. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  90. if (tf->ctl != ap->last_ctl) {
  91. writeb(tf->ctl, ioaddr->ctl_addr);
  92. ap->last_ctl = tf->ctl;
  93. ata_wait_idle(ap);
  94. }
  95. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  96. writew(tf->feature | (((u16)tf->hob_feature) << 8), ioaddr->feature_addr);
  97. writew(tf->nsect | (((u16)tf->hob_nsect) << 8), ioaddr->nsect_addr);
  98. writew(tf->lbal | (((u16)tf->hob_lbal) << 8), ioaddr->lbal_addr);
  99. writew(tf->lbam | (((u16)tf->hob_lbam) << 8), ioaddr->lbam_addr);
  100. writew(tf->lbah | (((u16)tf->hob_lbah) << 8), ioaddr->lbah_addr);
  101. } else if (is_addr) {
  102. writew(tf->feature, ioaddr->feature_addr);
  103. writew(tf->nsect, ioaddr->nsect_addr);
  104. writew(tf->lbal, ioaddr->lbal_addr);
  105. writew(tf->lbam, ioaddr->lbam_addr);
  106. writew(tf->lbah, ioaddr->lbah_addr);
  107. }
  108. if (tf->flags & ATA_TFLAG_DEVICE)
  109. writeb(tf->device, ioaddr->device_addr);
  110. ata_wait_idle(ap);
  111. }
  112. static void k2_sata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  113. {
  114. struct ata_ioports *ioaddr = &ap->ioaddr;
  115. u16 nsect, lbal, lbam, lbah;
  116. nsect = tf->nsect = readw(ioaddr->nsect_addr);
  117. lbal = tf->lbal = readw(ioaddr->lbal_addr);
  118. lbam = tf->lbam = readw(ioaddr->lbam_addr);
  119. lbah = tf->lbah = readw(ioaddr->lbah_addr);
  120. tf->device = readw(ioaddr->device_addr);
  121. if (tf->flags & ATA_TFLAG_LBA48) {
  122. tf->hob_feature = readw(ioaddr->error_addr) >> 8;
  123. tf->hob_nsect = nsect >> 8;
  124. tf->hob_lbal = lbal >> 8;
  125. tf->hob_lbam = lbam >> 8;
  126. tf->hob_lbah = lbah >> 8;
  127. }
  128. }
  129. /**
  130. * k2_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction (MMIO)
  131. * @qc: Info associated with this ATA transaction.
  132. *
  133. * LOCKING:
  134. * spin_lock_irqsave(host_set lock)
  135. */
  136. static void k2_bmdma_setup_mmio (struct ata_queued_cmd *qc)
  137. {
  138. struct ata_port *ap = qc->ap;
  139. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  140. u8 dmactl;
  141. void *mmio = (void *) ap->ioaddr.bmdma_addr;
  142. /* load PRD table addr. */
  143. mb(); /* make sure PRD table writes are visible to controller */
  144. writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
  145. /* specify data direction, triple-check start bit is clear */
  146. dmactl = readb(mmio + ATA_DMA_CMD);
  147. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  148. if (!rw)
  149. dmactl |= ATA_DMA_WR;
  150. writeb(dmactl, mmio + ATA_DMA_CMD);
  151. /* issue r/w command if this is not a ATA DMA command*/
  152. if (qc->tf.protocol != ATA_PROT_DMA)
  153. ap->ops->exec_command(ap, &qc->tf);
  154. }
  155. /**
  156. * k2_bmdma_start_mmio - Start a PCI IDE BMDMA transaction (MMIO)
  157. * @qc: Info associated with this ATA transaction.
  158. *
  159. * LOCKING:
  160. * spin_lock_irqsave(host_set lock)
  161. */
  162. static void k2_bmdma_start_mmio (struct ata_queued_cmd *qc)
  163. {
  164. struct ata_port *ap = qc->ap;
  165. void *mmio = (void *) ap->ioaddr.bmdma_addr;
  166. u8 dmactl;
  167. /* start host DMA transaction */
  168. dmactl = readb(mmio + ATA_DMA_CMD);
  169. writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
  170. /* There is a race condition in certain SATA controllers that can
  171. be seen when the r/w command is given to the controller before the
  172. host DMA is started. On a Read command, the controller would initiate
  173. the command to the drive even before it sees the DMA start. When there
  174. are very fast drives connected to the controller, or when the data request
  175. hits in the drive cache, there is the possibility that the drive returns a part
  176. or all of the requested data to the controller before the DMA start is issued.
  177. In this case, the controller would become confused as to what to do with the data.
  178. In the worst case when all the data is returned back to the controller, the
  179. controller could hang. In other cases it could return partial data returning
  180. in data corruption. This problem has been seen in PPC systems and can also appear
  181. on an system with very fast disks, where the SATA controller is sitting behind a
  182. number of bridges, and hence there is significant latency between the r/w command
  183. and the start command. */
  184. /* issue r/w command if the access is to ATA*/
  185. if (qc->tf.protocol == ATA_PROT_DMA)
  186. ap->ops->exec_command(ap, &qc->tf);
  187. }
  188. static u8 k2_stat_check_status(struct ata_port *ap)
  189. {
  190. return readl((void *) ap->ioaddr.status_addr);
  191. }
  192. #ifdef CONFIG_PPC_OF
  193. /*
  194. * k2_sata_proc_info
  195. * inout : decides on the direction of the dataflow and the meaning of the
  196. * variables
  197. * buffer: If inout==FALSE data is being written to it else read from it
  198. * *start: If inout==FALSE start of the valid data in the buffer
  199. * offset: If inout==FALSE offset from the beginning of the imaginary file
  200. * from which we start writing into the buffer
  201. * length: If inout==FALSE max number of bytes to be written into the buffer
  202. * else number of bytes in the buffer
  203. */
  204. static int k2_sata_proc_info(struct Scsi_Host *shost, char *page, char **start,
  205. off_t offset, int count, int inout)
  206. {
  207. struct ata_port *ap;
  208. struct device_node *np;
  209. int len, index;
  210. /* Find the ata_port */
  211. ap = (struct ata_port *) &shost->hostdata[0];
  212. if (ap == NULL)
  213. return 0;
  214. /* Find the OF node for the PCI device proper */
  215. np = pci_device_to_OF_node(to_pci_dev(ap->host_set->dev));
  216. if (np == NULL)
  217. return 0;
  218. /* Match it to a port node */
  219. index = (ap == ap->host_set->ports[0]) ? 0 : 1;
  220. for (np = np->child; np != NULL; np = np->sibling) {
  221. u32 *reg = (u32 *)get_property(np, "reg", NULL);
  222. if (!reg)
  223. continue;
  224. if (index == *reg)
  225. break;
  226. }
  227. if (np == NULL)
  228. return 0;
  229. len = sprintf(page, "devspec: %s\n", np->full_name);
  230. return len;
  231. }
  232. #endif /* CONFIG_PPC_OF */
  233. static Scsi_Host_Template k2_sata_sht = {
  234. .module = THIS_MODULE,
  235. .name = DRV_NAME,
  236. .ioctl = ata_scsi_ioctl,
  237. .queuecommand = ata_scsi_queuecmd,
  238. .eh_strategy_handler = ata_scsi_error,
  239. .can_queue = ATA_DEF_QUEUE,
  240. .this_id = ATA_SHT_THIS_ID,
  241. .sg_tablesize = LIBATA_MAX_PRD,
  242. .max_sectors = ATA_MAX_SECTORS,
  243. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  244. .emulated = ATA_SHT_EMULATED,
  245. .use_clustering = ATA_SHT_USE_CLUSTERING,
  246. .proc_name = DRV_NAME,
  247. .dma_boundary = ATA_DMA_BOUNDARY,
  248. .slave_configure = ata_scsi_slave_config,
  249. #ifdef CONFIG_PPC_OF
  250. .proc_info = k2_sata_proc_info,
  251. #endif
  252. .bios_param = ata_std_bios_param,
  253. .ordered_flush = 1,
  254. };
  255. static struct ata_port_operations k2_sata_ops = {
  256. .port_disable = ata_port_disable,
  257. .tf_load = k2_sata_tf_load,
  258. .tf_read = k2_sata_tf_read,
  259. .check_status = k2_stat_check_status,
  260. .exec_command = ata_exec_command,
  261. .dev_select = ata_std_dev_select,
  262. .phy_reset = sata_phy_reset,
  263. .bmdma_setup = k2_bmdma_setup_mmio,
  264. .bmdma_start = k2_bmdma_start_mmio,
  265. .bmdma_stop = ata_bmdma_stop,
  266. .bmdma_status = ata_bmdma_status,
  267. .qc_prep = ata_qc_prep,
  268. .qc_issue = ata_qc_issue_prot,
  269. .eng_timeout = ata_eng_timeout,
  270. .irq_handler = ata_interrupt,
  271. .irq_clear = ata_bmdma_irq_clear,
  272. .scr_read = k2_sata_scr_read,
  273. .scr_write = k2_sata_scr_write,
  274. .port_start = ata_port_start,
  275. .port_stop = ata_port_stop,
  276. .host_stop = ata_host_stop,
  277. };
  278. static void k2_sata_setup_port(struct ata_ioports *port, unsigned long base)
  279. {
  280. port->cmd_addr = base + K2_SATA_TF_CMD_OFFSET;
  281. port->data_addr = base + K2_SATA_TF_DATA_OFFSET;
  282. port->feature_addr =
  283. port->error_addr = base + K2_SATA_TF_ERROR_OFFSET;
  284. port->nsect_addr = base + K2_SATA_TF_NSECT_OFFSET;
  285. port->lbal_addr = base + K2_SATA_TF_LBAL_OFFSET;
  286. port->lbam_addr = base + K2_SATA_TF_LBAM_OFFSET;
  287. port->lbah_addr = base + K2_SATA_TF_LBAH_OFFSET;
  288. port->device_addr = base + K2_SATA_TF_DEVICE_OFFSET;
  289. port->command_addr =
  290. port->status_addr = base + K2_SATA_TF_CMDSTAT_OFFSET;
  291. port->altstatus_addr =
  292. port->ctl_addr = base + K2_SATA_TF_CTL_OFFSET;
  293. port->bmdma_addr = base + K2_SATA_DMA_CMD_OFFSET;
  294. port->scr_addr = base + K2_SATA_SCR_STATUS_OFFSET;
  295. }
  296. static int k2_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  297. {
  298. static int printed_version;
  299. struct ata_probe_ent *probe_ent = NULL;
  300. unsigned long base;
  301. void *mmio_base;
  302. int pci_dev_busy = 0;
  303. int rc;
  304. int i;
  305. if (!printed_version++)
  306. printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  307. /*
  308. * If this driver happens to only be useful on Apple's K2, then
  309. * we should check that here as it has a normal Serverworks ID
  310. */
  311. rc = pci_enable_device(pdev);
  312. if (rc)
  313. return rc;
  314. /*
  315. * Check if we have resources mapped at all (second function may
  316. * have been disabled by firmware)
  317. */
  318. if (pci_resource_len(pdev, 5) == 0)
  319. return -ENODEV;
  320. /* Request PCI regions */
  321. rc = pci_request_regions(pdev, DRV_NAME);
  322. if (rc) {
  323. pci_dev_busy = 1;
  324. goto err_out;
  325. }
  326. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  327. if (rc)
  328. goto err_out_regions;
  329. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  330. if (rc)
  331. goto err_out_regions;
  332. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  333. if (probe_ent == NULL) {
  334. rc = -ENOMEM;
  335. goto err_out_regions;
  336. }
  337. memset(probe_ent, 0, sizeof(*probe_ent));
  338. probe_ent->dev = pci_dev_to_dev(pdev);
  339. INIT_LIST_HEAD(&probe_ent->node);
  340. mmio_base = ioremap(pci_resource_start(pdev, 5),
  341. pci_resource_len(pdev, 5));
  342. if (mmio_base == NULL) {
  343. rc = -ENOMEM;
  344. goto err_out_free_ent;
  345. }
  346. base = (unsigned long) mmio_base;
  347. /* Clear a magic bit in SCR1 according to Darwin, those help
  348. * some funky seagate drives (though so far, those were already
  349. * set by the firmware on the machines I had access to)
  350. */
  351. writel(readl(mmio_base + K2_SATA_SICR1_OFFSET) & ~0x00040000,
  352. mmio_base + K2_SATA_SICR1_OFFSET);
  353. /* Clear SATA error & interrupts we don't use */
  354. writel(0xffffffff, mmio_base + K2_SATA_SCR_ERROR_OFFSET);
  355. writel(0x0, mmio_base + K2_SATA_SIM_OFFSET);
  356. probe_ent->sht = &k2_sata_sht;
  357. probe_ent->host_flags = ATA_FLAG_SATA | ATA_FLAG_SATA_RESET |
  358. ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
  359. probe_ent->port_ops = &k2_sata_ops;
  360. probe_ent->n_ports = 4;
  361. probe_ent->irq = pdev->irq;
  362. probe_ent->irq_flags = SA_SHIRQ;
  363. probe_ent->mmio_base = mmio_base;
  364. /* We don't care much about the PIO/UDMA masks, but the core won't like us
  365. * if we don't fill these
  366. */
  367. probe_ent->pio_mask = 0x1f;
  368. probe_ent->mwdma_mask = 0x7;
  369. probe_ent->udma_mask = 0x7f;
  370. /* different controllers have different number of ports - currently 4 or 8 */
  371. /* All ports are on the same function. Multi-function device is no
  372. * longer available. This should not be seen in any system. */
  373. for (i = 0; i < ent->driver_data; i++)
  374. k2_sata_setup_port(&probe_ent->port[i], base + i * K2_SATA_PORT_OFFSET);
  375. pci_set_master(pdev);
  376. /* FIXME: check ata_device_add return value */
  377. ata_device_add(probe_ent);
  378. kfree(probe_ent);
  379. return 0;
  380. err_out_free_ent:
  381. kfree(probe_ent);
  382. err_out_regions:
  383. pci_release_regions(pdev);
  384. err_out:
  385. if (!pci_dev_busy)
  386. pci_disable_device(pdev);
  387. return rc;
  388. }
  389. /* 0x240 is device ID for Apple K2 device
  390. * 0x241 is device ID for Serverworks Frodo4
  391. * 0x242 is device ID for Serverworks Frodo8
  392. * 0x24a is device ID for BCM5785 (aka HT1000) HT southbridge integrated SATA
  393. * controller
  394. * */
  395. static struct pci_device_id k2_sata_pci_tbl[] = {
  396. { 0x1166, 0x0240, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
  397. { 0x1166, 0x0241, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
  398. { 0x1166, 0x0242, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
  399. { 0x1166, 0x024a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
  400. { }
  401. };
  402. static struct pci_driver k2_sata_pci_driver = {
  403. .name = DRV_NAME,
  404. .id_table = k2_sata_pci_tbl,
  405. .probe = k2_sata_init_one,
  406. .remove = ata_pci_remove_one,
  407. };
  408. static int __init k2_sata_init(void)
  409. {
  410. return pci_module_init(&k2_sata_pci_driver);
  411. }
  412. static void __exit k2_sata_exit(void)
  413. {
  414. pci_unregister_driver(&k2_sata_pci_driver);
  415. }
  416. MODULE_AUTHOR("Benjamin Herrenschmidt");
  417. MODULE_DESCRIPTION("low-level driver for K2 SATA controller");
  418. MODULE_LICENSE("GPL");
  419. MODULE_DEVICE_TABLE(pci, k2_sata_pci_tbl);
  420. MODULE_VERSION(DRV_VERSION);
  421. module_init(k2_sata_init);
  422. module_exit(k2_sata_exit);