pata_serverworks.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * pata_serverworks.c - Serverworks PATA for new ATA layer
  4. * (C) 2005 Red Hat Inc
  5. * (C) 2010 Bartlomiej Zolnierkiewicz
  6. *
  7. * based upon
  8. *
  9. * serverworks.c
  10. *
  11. * Copyright (C) 1998-2000 Michel Aubry
  12. * Copyright (C) 1998-2000 Andrzej Krzysztofowicz
  13. * Copyright (C) 1998-2000 Andre Hedrick <[email protected]>
  14. * Portions copyright (c) 2001 Sun Microsystems
  15. *
  16. *
  17. * RCC/ServerWorks IDE driver for Linux
  18. *
  19. * OSB4: `Open South Bridge' IDE Interface (fn 1)
  20. * supports UDMA mode 2 (33 MB/s)
  21. *
  22. * CSB5: `Champion South Bridge' IDE Interface (fn 1)
  23. * all revisions support UDMA mode 4 (66 MB/s)
  24. * revision A2.0 and up support UDMA mode 5 (100 MB/s)
  25. *
  26. * *** The CSB5 does not provide ANY register ***
  27. * *** to detect 80-conductor cable presence. ***
  28. *
  29. * CSB6: `Champion South Bridge' IDE Interface (optional: third channel)
  30. *
  31. * Documentation:
  32. * Available under NDA only. Errata info very hard to get.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/delay.h>
  39. #include <scsi/scsi_host.h>
  40. #include <linux/libata.h>
  41. #define DRV_NAME "pata_serverworks"
  42. #define DRV_VERSION "0.4.3"
  43. #define SVWKS_CSB5_REVISION_NEW 0x92 /* min PCI_REVISION_ID for UDMA5 (A2.0) */
  44. #define SVWKS_CSB6_REVISION 0xa0 /* min PCI_REVISION_ID for UDMA4 (A1.0) */
  45. /* Seagate Barracuda ATA IV Family drives in UDMA mode 5
  46. * can overrun their FIFOs when used with the CSB5 */
  47. static const char *csb_bad_ata100[] = {
  48. "ST320011A",
  49. "ST340016A",
  50. "ST360021A",
  51. "ST380021A",
  52. NULL
  53. };
  54. /**
  55. * oem_cable - Dell/Sun serverworks cable detection
  56. * @ap: ATA port to do cable detect
  57. *
  58. * Dell PowerEdge and Sun Cobalt 'Alpine' hide the 40/80 pin select
  59. * for their interfaces in the top two bits of the subsystem ID.
  60. */
  61. static int oem_cable(struct ata_port *ap)
  62. {
  63. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  64. if (pdev->subsystem_device & (1 << (ap->port_no + 14)))
  65. return ATA_CBL_PATA80;
  66. return ATA_CBL_PATA40;
  67. }
  68. struct sv_cable_table {
  69. int device;
  70. int subvendor;
  71. int (*cable_detect)(struct ata_port *ap);
  72. };
  73. static struct sv_cable_table cable_detect[] = {
  74. { PCI_DEVICE_ID_SERVERWORKS_CSB5IDE, PCI_VENDOR_ID_DELL, oem_cable },
  75. { PCI_DEVICE_ID_SERVERWORKS_CSB6IDE, PCI_VENDOR_ID_DELL, oem_cable },
  76. { PCI_DEVICE_ID_SERVERWORKS_CSB5IDE, PCI_VENDOR_ID_SUN, oem_cable },
  77. { PCI_DEVICE_ID_SERVERWORKS_OSB4IDE, PCI_ANY_ID, ata_cable_40wire },
  78. { PCI_DEVICE_ID_SERVERWORKS_CSB5IDE, PCI_ANY_ID, ata_cable_unknown },
  79. { PCI_DEVICE_ID_SERVERWORKS_CSB6IDE, PCI_ANY_ID, ata_cable_unknown },
  80. { PCI_DEVICE_ID_SERVERWORKS_CSB6IDE2, PCI_ANY_ID, ata_cable_unknown },
  81. { PCI_DEVICE_ID_SERVERWORKS_HT1000IDE, PCI_ANY_ID, ata_cable_unknown },
  82. { }
  83. };
  84. /**
  85. * serverworks_cable_detect - cable detection
  86. * @ap: ATA port
  87. *
  88. * Perform cable detection according to the device and subvendor
  89. * identifications
  90. */
  91. static int serverworks_cable_detect(struct ata_port *ap)
  92. {
  93. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  94. struct sv_cable_table *cb = cable_detect;
  95. while(cb->device) {
  96. if (cb->device == pdev->device &&
  97. (cb->subvendor == pdev->subsystem_vendor ||
  98. cb->subvendor == PCI_ANY_ID)) {
  99. return cb->cable_detect(ap);
  100. }
  101. cb++;
  102. }
  103. BUG();
  104. return -1; /* kill compiler warning */
  105. }
  106. /**
  107. * serverworks_is_csb - Check for CSB or OSB
  108. * @pdev: PCI device to check
  109. *
  110. * Returns true if the device being checked is known to be a CSB
  111. * series device.
  112. */
  113. static u8 serverworks_is_csb(struct pci_dev *pdev)
  114. {
  115. switch (pdev->device) {
  116. case PCI_DEVICE_ID_SERVERWORKS_CSB5IDE:
  117. case PCI_DEVICE_ID_SERVERWORKS_CSB6IDE:
  118. case PCI_DEVICE_ID_SERVERWORKS_CSB6IDE2:
  119. case PCI_DEVICE_ID_SERVERWORKS_HT1000IDE:
  120. return 1;
  121. default:
  122. break;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * serverworks_osb4_filter - mode selection filter
  128. * @adev: ATA device
  129. * @mask: Mask of proposed modes
  130. *
  131. * Filter the offered modes for the device to apply controller
  132. * specific rules. OSB4 requires no UDMA for disks due to a FIFO
  133. * bug we hit.
  134. */
  135. static unsigned int serverworks_osb4_filter(struct ata_device *adev, unsigned int mask)
  136. {
  137. if (adev->class == ATA_DEV_ATA)
  138. mask &= ~ATA_MASK_UDMA;
  139. return mask;
  140. }
  141. /**
  142. * serverworks_csb_filter - mode selection filter
  143. * @adev: ATA device
  144. * @mask: Mask of proposed modes
  145. *
  146. * Check the blacklist and disable UDMA5 if matched
  147. */
  148. static unsigned int serverworks_csb_filter(struct ata_device *adev, unsigned int mask)
  149. {
  150. const char *p;
  151. char model_num[ATA_ID_PROD_LEN + 1];
  152. int i;
  153. /* Disk, UDMA */
  154. if (adev->class != ATA_DEV_ATA)
  155. return mask;
  156. /* Actually do need to check */
  157. ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  158. for (i = 0; (p = csb_bad_ata100[i]) != NULL; i++) {
  159. if (!strcmp(p, model_num))
  160. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  161. }
  162. return mask;
  163. }
  164. /**
  165. * serverworks_set_piomode - set initial PIO mode data
  166. * @ap: ATA interface
  167. * @adev: ATA device
  168. *
  169. * Program the OSB4/CSB5 timing registers for PIO. The PIO register
  170. * load is done as a simple lookup.
  171. */
  172. static void serverworks_set_piomode(struct ata_port *ap, struct ata_device *adev)
  173. {
  174. static const u8 pio_mode[] = { 0x5d, 0x47, 0x34, 0x22, 0x20 };
  175. int offset = 1 + 2 * ap->port_no - adev->devno;
  176. int devbits = (2 * ap->port_no + adev->devno) * 4;
  177. u16 csb5_pio;
  178. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  179. int pio = adev->pio_mode - XFER_PIO_0;
  180. pci_write_config_byte(pdev, 0x40 + offset, pio_mode[pio]);
  181. /* The OSB4 just requires the timing but the CSB series want the
  182. mode number as well */
  183. if (serverworks_is_csb(pdev)) {
  184. pci_read_config_word(pdev, 0x4A, &csb5_pio);
  185. csb5_pio &= ~(0x0F << devbits);
  186. pci_write_config_word(pdev, 0x4A, csb5_pio | (pio << devbits));
  187. }
  188. }
  189. /**
  190. * serverworks_set_dmamode - set initial DMA mode data
  191. * @ap: ATA interface
  192. * @adev: ATA device
  193. *
  194. * Program the MWDMA/UDMA modes for the serverworks OSB4/CSB5
  195. * chipset. The MWDMA mode values are pulled from a lookup table
  196. * while the chipset uses mode number for UDMA.
  197. */
  198. static void serverworks_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  199. {
  200. static const u8 dma_mode[] = { 0x77, 0x21, 0x20 };
  201. int offset = 1 + 2 * ap->port_no - adev->devno;
  202. int devbits = 2 * ap->port_no + adev->devno;
  203. u8 ultra;
  204. u8 ultra_cfg;
  205. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  206. pci_read_config_byte(pdev, 0x54, &ultra_cfg);
  207. pci_read_config_byte(pdev, 0x56 + ap->port_no, &ultra);
  208. ultra &= ~(0x0F << (adev->devno * 4));
  209. if (adev->dma_mode >= XFER_UDMA_0) {
  210. pci_write_config_byte(pdev, 0x44 + offset, 0x20);
  211. ultra |= (adev->dma_mode - XFER_UDMA_0)
  212. << (adev->devno * 4);
  213. ultra_cfg |= (1 << devbits);
  214. } else {
  215. pci_write_config_byte(pdev, 0x44 + offset,
  216. dma_mode[adev->dma_mode - XFER_MW_DMA_0]);
  217. ultra_cfg &= ~(1 << devbits);
  218. }
  219. pci_write_config_byte(pdev, 0x56 + ap->port_no, ultra);
  220. pci_write_config_byte(pdev, 0x54, ultra_cfg);
  221. }
  222. static struct scsi_host_template serverworks_osb4_sht = {
  223. ATA_BASE_SHT(DRV_NAME),
  224. .sg_tablesize = LIBATA_DUMB_MAX_PRD,
  225. .dma_boundary = ATA_DMA_BOUNDARY,
  226. };
  227. static struct scsi_host_template serverworks_csb_sht = {
  228. ATA_BMDMA_SHT(DRV_NAME),
  229. };
  230. static struct ata_port_operations serverworks_osb4_port_ops = {
  231. .inherits = &ata_bmdma_port_ops,
  232. .qc_prep = ata_bmdma_dumb_qc_prep,
  233. .cable_detect = serverworks_cable_detect,
  234. .mode_filter = serverworks_osb4_filter,
  235. .set_piomode = serverworks_set_piomode,
  236. .set_dmamode = serverworks_set_dmamode,
  237. };
  238. static struct ata_port_operations serverworks_csb_port_ops = {
  239. .inherits = &serverworks_osb4_port_ops,
  240. .qc_prep = ata_bmdma_qc_prep,
  241. .mode_filter = serverworks_csb_filter,
  242. };
  243. static int serverworks_fixup_osb4(struct pci_dev *pdev)
  244. {
  245. u32 reg;
  246. struct pci_dev *isa_dev = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  247. PCI_DEVICE_ID_SERVERWORKS_OSB4, NULL);
  248. if (isa_dev) {
  249. pci_read_config_dword(isa_dev, 0x64, &reg);
  250. reg &= ~0x00002000; /* disable 600ns interrupt mask */
  251. if (!(reg & 0x00004000))
  252. dev_info(&pdev->dev, "UDMA not BIOS enabled.\n");
  253. reg |= 0x00004000; /* enable UDMA/33 support */
  254. pci_write_config_dword(isa_dev, 0x64, reg);
  255. pci_dev_put(isa_dev);
  256. return 0;
  257. }
  258. dev_warn(&pdev->dev, "Unable to find bridge.\n");
  259. return -ENODEV;
  260. }
  261. static int serverworks_fixup_csb(struct pci_dev *pdev)
  262. {
  263. u8 btr;
  264. /* Third Channel Test */
  265. if (!(PCI_FUNC(pdev->devfn) & 1)) {
  266. struct pci_dev * findev = NULL;
  267. u32 reg4c = 0;
  268. findev = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  269. PCI_DEVICE_ID_SERVERWORKS_CSB5, NULL);
  270. if (findev) {
  271. pci_read_config_dword(findev, 0x4C, &reg4c);
  272. reg4c &= ~0x000007FF;
  273. reg4c |= 0x00000040;
  274. reg4c |= 0x00000020;
  275. pci_write_config_dword(findev, 0x4C, reg4c);
  276. pci_dev_put(findev);
  277. }
  278. } else {
  279. struct pci_dev * findev = NULL;
  280. u8 reg41 = 0;
  281. findev = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  282. PCI_DEVICE_ID_SERVERWORKS_CSB6, NULL);
  283. if (findev) {
  284. pci_read_config_byte(findev, 0x41, &reg41);
  285. reg41 &= ~0x40;
  286. pci_write_config_byte(findev, 0x41, reg41);
  287. pci_dev_put(findev);
  288. }
  289. }
  290. /* setup the UDMA Control register
  291. *
  292. * 1. clear bit 6 to enable DMA
  293. * 2. enable DMA modes with bits 0-1
  294. * 00 : legacy
  295. * 01 : udma2
  296. * 10 : udma2/udma4
  297. * 11 : udma2/udma4/udma5
  298. */
  299. pci_read_config_byte(pdev, 0x5A, &btr);
  300. btr &= ~0x40;
  301. if (!(PCI_FUNC(pdev->devfn) & 1))
  302. btr |= 0x2;
  303. else
  304. btr |= (pdev->revision >= SVWKS_CSB5_REVISION_NEW) ? 0x3 : 0x2;
  305. pci_write_config_byte(pdev, 0x5A, btr);
  306. return btr;
  307. }
  308. static void serverworks_fixup_ht1000(struct pci_dev *pdev)
  309. {
  310. u8 btr;
  311. /* Setup HT1000 SouthBridge Controller - Single Channel Only */
  312. pci_read_config_byte(pdev, 0x5A, &btr);
  313. btr &= ~0x40;
  314. btr |= 0x3;
  315. pci_write_config_byte(pdev, 0x5A, btr);
  316. }
  317. static int serverworks_fixup(struct pci_dev *pdev)
  318. {
  319. int rc = 0;
  320. /* Force master latency timer to 64 PCI clocks */
  321. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x40);
  322. switch (pdev->device) {
  323. case PCI_DEVICE_ID_SERVERWORKS_OSB4IDE:
  324. rc = serverworks_fixup_osb4(pdev);
  325. break;
  326. case PCI_DEVICE_ID_SERVERWORKS_CSB5IDE:
  327. ata_pci_bmdma_clear_simplex(pdev);
  328. fallthrough;
  329. case PCI_DEVICE_ID_SERVERWORKS_CSB6IDE:
  330. case PCI_DEVICE_ID_SERVERWORKS_CSB6IDE2:
  331. rc = serverworks_fixup_csb(pdev);
  332. break;
  333. case PCI_DEVICE_ID_SERVERWORKS_HT1000IDE:
  334. serverworks_fixup_ht1000(pdev);
  335. break;
  336. }
  337. return rc;
  338. }
  339. static int serverworks_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  340. {
  341. static const struct ata_port_info info[4] = {
  342. { /* OSB4 */
  343. .flags = ATA_FLAG_SLAVE_POSS,
  344. .pio_mask = ATA_PIO4,
  345. .mwdma_mask = ATA_MWDMA2,
  346. .udma_mask = ATA_UDMA2,
  347. .port_ops = &serverworks_osb4_port_ops
  348. }, { /* OSB4 no UDMA */
  349. .flags = ATA_FLAG_SLAVE_POSS,
  350. .pio_mask = ATA_PIO4,
  351. .mwdma_mask = ATA_MWDMA2,
  352. /* No UDMA */
  353. .port_ops = &serverworks_osb4_port_ops
  354. }, { /* CSB5 */
  355. .flags = ATA_FLAG_SLAVE_POSS,
  356. .pio_mask = ATA_PIO4,
  357. .mwdma_mask = ATA_MWDMA2,
  358. .udma_mask = ATA_UDMA4,
  359. .port_ops = &serverworks_csb_port_ops
  360. }, { /* CSB5 - later revisions*/
  361. .flags = ATA_FLAG_SLAVE_POSS,
  362. .pio_mask = ATA_PIO4,
  363. .mwdma_mask = ATA_MWDMA2,
  364. .udma_mask = ATA_UDMA5,
  365. .port_ops = &serverworks_csb_port_ops
  366. }
  367. };
  368. const struct ata_port_info *ppi[] = { &info[id->driver_data], NULL };
  369. struct scsi_host_template *sht = &serverworks_csb_sht;
  370. int rc;
  371. rc = pcim_enable_device(pdev);
  372. if (rc)
  373. return rc;
  374. rc = serverworks_fixup(pdev);
  375. /* OSB4 : South Bridge and IDE */
  376. if (pdev->device == PCI_DEVICE_ID_SERVERWORKS_OSB4IDE) {
  377. /* Select non UDMA capable OSB4 if we can't do fixups */
  378. if (rc < 0)
  379. ppi[0] = &info[1];
  380. sht = &serverworks_osb4_sht;
  381. }
  382. /* setup CSB5/CSB6 : South Bridge and IDE option RAID */
  383. else if ((pdev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5IDE) ||
  384. (pdev->device == PCI_DEVICE_ID_SERVERWORKS_CSB6IDE) ||
  385. (pdev->device == PCI_DEVICE_ID_SERVERWORKS_CSB6IDE2)) {
  386. /* If the returned btr is the newer revision then
  387. select the right info block */
  388. if (rc == 3)
  389. ppi[0] = &info[3];
  390. /* Is this the 3rd channel CSB6 IDE ? */
  391. if (pdev->device == PCI_DEVICE_ID_SERVERWORKS_CSB6IDE2)
  392. ppi[1] = &ata_dummy_port_info;
  393. }
  394. return ata_pci_bmdma_init_one(pdev, ppi, sht, NULL, 0);
  395. }
  396. #ifdef CONFIG_PM_SLEEP
  397. static int serverworks_reinit_one(struct pci_dev *pdev)
  398. {
  399. struct ata_host *host = pci_get_drvdata(pdev);
  400. int rc;
  401. rc = ata_pci_device_do_resume(pdev);
  402. if (rc)
  403. return rc;
  404. (void)serverworks_fixup(pdev);
  405. ata_host_resume(host);
  406. return 0;
  407. }
  408. #endif
  409. static const struct pci_device_id serverworks[] = {
  410. { PCI_VDEVICE(SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4IDE), 0},
  411. { PCI_VDEVICE(SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5IDE), 2},
  412. { PCI_VDEVICE(SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB6IDE), 2},
  413. { PCI_VDEVICE(SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB6IDE2), 2},
  414. { PCI_VDEVICE(SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_HT1000IDE), 2},
  415. { },
  416. };
  417. static struct pci_driver serverworks_pci_driver = {
  418. .name = DRV_NAME,
  419. .id_table = serverworks,
  420. .probe = serverworks_init_one,
  421. .remove = ata_pci_remove_one,
  422. #ifdef CONFIG_PM_SLEEP
  423. .suspend = ata_pci_device_suspend,
  424. .resume = serverworks_reinit_one,
  425. #endif
  426. };
  427. module_pci_driver(serverworks_pci_driver);
  428. MODULE_AUTHOR("Alan Cox");
  429. MODULE_DESCRIPTION("low-level driver for Serverworks OSB4/CSB5/CSB6");
  430. MODULE_LICENSE("GPL");
  431. MODULE_DEVICE_TABLE(pci, serverworks);
  432. MODULE_VERSION(DRV_VERSION);