sc1200.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * linux/drivers/ide/pci/sc1200.c Version 0.97 Aug 3 2007
  3. *
  4. * Copyright (C) 2000-2002 Mark Lord <mlord@pobox.com>
  5. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  6. *
  7. * May be copied or modified under the terms of the GNU General Public License
  8. *
  9. * Development of this chipset driver was funded
  10. * by the nice folks at National Semiconductor.
  11. *
  12. * Documentation:
  13. * Available from National Semiconductor
  14. */
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/delay.h>
  19. #include <linux/timer.h>
  20. #include <linux/mm.h>
  21. #include <linux/ioport.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/hdreg.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/init.h>
  27. #include <linux/ide.h>
  28. #include <linux/pm.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #define SC1200_REV_A 0x00
  32. #define SC1200_REV_B1 0x01
  33. #define SC1200_REV_B3 0x02
  34. #define SC1200_REV_C1 0x03
  35. #define SC1200_REV_D1 0x04
  36. #define PCI_CLK_33 0x00
  37. #define PCI_CLK_48 0x01
  38. #define PCI_CLK_66 0x02
  39. #define PCI_CLK_33A 0x03
  40. static unsigned short sc1200_get_pci_clock (void)
  41. {
  42. unsigned char chip_id, silicon_revision;
  43. unsigned int pci_clock;
  44. /*
  45. * Check the silicon revision, as not all versions of the chip
  46. * have the register with the fast PCI bus timings.
  47. */
  48. chip_id = inb (0x903c);
  49. silicon_revision = inb (0x903d);
  50. // Read the fast pci clock frequency
  51. if (chip_id == 0x04 && silicon_revision < SC1200_REV_B1) {
  52. pci_clock = PCI_CLK_33;
  53. } else {
  54. // check clock generator configuration (cfcc)
  55. // the clock is in bits 8 and 9 of this word
  56. pci_clock = inw (0x901e);
  57. pci_clock >>= 8;
  58. pci_clock &= 0x03;
  59. if (pci_clock == PCI_CLK_33A)
  60. pci_clock = PCI_CLK_33;
  61. }
  62. return pci_clock;
  63. }
  64. /*
  65. * Here are the standard PIO mode 0-4 timings for each "format".
  66. * Format-0 uses fast data reg timings, with slower command reg timings.
  67. * Format-1 uses fast timings for all registers, but won't work with all drives.
  68. */
  69. static const unsigned int sc1200_pio_timings[4][5] =
  70. {{0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010}, // format0 33Mhz
  71. {0xd1329172, 0x71212171, 0x30200080, 0x20102010, 0x00100010}, // format1, 33Mhz
  72. {0xfaa3f4f3, 0xc23232b2, 0x513101c1, 0x31213121, 0x10211021}, // format1, 48Mhz
  73. {0xfff4fff4, 0xf35353d3, 0x814102f1, 0x42314231, 0x11311131}}; // format1, 66Mhz
  74. /*
  75. * After chip reset, the PIO timings are set to 0x00009172, which is not valid.
  76. */
  77. //#define SC1200_BAD_PIO(timings) (((timings)&~0x80000000)==0x00009172)
  78. static void sc1200_tunepio(ide_drive_t *drive, u8 pio)
  79. {
  80. ide_hwif_t *hwif = drive->hwif;
  81. struct pci_dev *pdev = hwif->pci_dev;
  82. unsigned int basereg = hwif->channel ? 0x50 : 0x40, format = 0;
  83. pci_read_config_dword(pdev, basereg + 4, &format);
  84. format = (format >> 31) & 1;
  85. if (format)
  86. format += sc1200_get_pci_clock();
  87. pci_write_config_dword(pdev, basereg + ((drive->dn & 1) << 3),
  88. sc1200_pio_timings[format][pio]);
  89. }
  90. /*
  91. * The SC1200 specifies that two drives sharing a cable cannot mix
  92. * UDMA/MDMA. It has to be one or the other, for the pair, though
  93. * different timings can still be chosen for each drive. We could
  94. * set the appropriate timing bits on the fly, but that might be
  95. * a bit confusing. So, for now we statically handle this requirement
  96. * by looking at our mate drive to see what it is capable of, before
  97. * choosing a mode for our own drive.
  98. */
  99. static u8 sc1200_udma_filter(ide_drive_t *drive)
  100. {
  101. ide_hwif_t *hwif = drive->hwif;
  102. ide_drive_t *mate = &hwif->drives[(drive->dn & 1) ^ 1];
  103. struct hd_driveid *mateid = mate->id;
  104. u8 mask = hwif->ultra_mask;
  105. if (mate->present == 0)
  106. goto out;
  107. if ((mateid->capability & 1) && __ide_dma_bad_drive(mate) == 0) {
  108. if ((mateid->field_valid & 4) && (mateid->dma_ultra & 7))
  109. goto out;
  110. if ((mateid->field_valid & 2) && (mateid->dma_mword & 7))
  111. mask = 0;
  112. }
  113. out:
  114. return mask;
  115. }
  116. static void sc1200_set_dma_mode(ide_drive_t *drive, const u8 mode)
  117. {
  118. ide_hwif_t *hwif = HWIF(drive);
  119. int unit = drive->select.b.unit;
  120. unsigned int reg, timings;
  121. unsigned short pci_clock;
  122. unsigned int basereg = hwif->channel ? 0x50 : 0x40;
  123. pci_clock = sc1200_get_pci_clock();
  124. /*
  125. * Note that each DMA mode has several timings associated with it.
  126. * The correct timing depends on the fast PCI clock freq.
  127. */
  128. timings = 0;
  129. switch (mode) {
  130. case XFER_UDMA_0:
  131. switch (pci_clock) {
  132. case PCI_CLK_33: timings = 0x00921250; break;
  133. case PCI_CLK_48: timings = 0x00932470; break;
  134. case PCI_CLK_66: timings = 0x009436a1; break;
  135. }
  136. break;
  137. case XFER_UDMA_1:
  138. switch (pci_clock) {
  139. case PCI_CLK_33: timings = 0x00911140; break;
  140. case PCI_CLK_48: timings = 0x00922260; break;
  141. case PCI_CLK_66: timings = 0x00933481; break;
  142. }
  143. break;
  144. case XFER_UDMA_2:
  145. switch (pci_clock) {
  146. case PCI_CLK_33: timings = 0x00911030; break;
  147. case PCI_CLK_48: timings = 0x00922140; break;
  148. case PCI_CLK_66: timings = 0x00923261; break;
  149. }
  150. break;
  151. case XFER_MW_DMA_0:
  152. switch (pci_clock) {
  153. case PCI_CLK_33: timings = 0x00077771; break;
  154. case PCI_CLK_48: timings = 0x000bbbb2; break;
  155. case PCI_CLK_66: timings = 0x000ffff3; break;
  156. }
  157. break;
  158. case XFER_MW_DMA_1:
  159. switch (pci_clock) {
  160. case PCI_CLK_33: timings = 0x00012121; break;
  161. case PCI_CLK_48: timings = 0x00024241; break;
  162. case PCI_CLK_66: timings = 0x00035352; break;
  163. }
  164. break;
  165. case XFER_MW_DMA_2:
  166. switch (pci_clock) {
  167. case PCI_CLK_33: timings = 0x00002020; break;
  168. case PCI_CLK_48: timings = 0x00013131; break;
  169. case PCI_CLK_66: timings = 0x00015151; break;
  170. }
  171. break;
  172. default:
  173. return;
  174. }
  175. if (unit == 0) { /* are we configuring drive0? */
  176. pci_read_config_dword(hwif->pci_dev, basereg+4, &reg);
  177. timings |= reg & 0x80000000; /* preserve PIO format bit */
  178. pci_write_config_dword(hwif->pci_dev, basereg+4, timings);
  179. } else {
  180. pci_write_config_dword(hwif->pci_dev, basereg+12, timings);
  181. }
  182. }
  183. /* Replacement for the standard ide_dma_end action in
  184. * dma_proc.
  185. *
  186. * returns 1 on error, 0 otherwise
  187. */
  188. static int sc1200_ide_dma_end (ide_drive_t *drive)
  189. {
  190. ide_hwif_t *hwif = HWIF(drive);
  191. unsigned long dma_base = hwif->dma_base;
  192. byte dma_stat;
  193. dma_stat = inb(dma_base+2); /* get DMA status */
  194. if (!(dma_stat & 4))
  195. printk(" ide_dma_end dma_stat=%0x err=%x newerr=%x\n",
  196. dma_stat, ((dma_stat&7)!=4), ((dma_stat&2)==2));
  197. outb(dma_stat|0x1b, dma_base+2); /* clear the INTR & ERROR bits */
  198. outb(inb(dma_base)&~1, dma_base); /* !! DO THIS HERE !! stop DMA */
  199. drive->waiting_for_dma = 0;
  200. ide_destroy_dmatable(drive); /* purge DMA mappings */
  201. return (dma_stat & 7) != 4; /* verify good DMA status */
  202. }
  203. /*
  204. * sc1200_set_pio_mode() handles setting of PIO modes
  205. * for both the chipset and drive.
  206. *
  207. * All existing BIOSs for this chipset guarantee that all drives
  208. * will have valid default PIO timings set up before we get here.
  209. */
  210. static void sc1200_set_pio_mode(ide_drive_t *drive, const u8 pio)
  211. {
  212. ide_hwif_t *hwif = HWIF(drive);
  213. int mode = -1;
  214. /*
  215. * bad abuse of ->set_pio_mode interface
  216. */
  217. switch (pio) {
  218. case 200: mode = XFER_UDMA_0; break;
  219. case 201: mode = XFER_UDMA_1; break;
  220. case 202: mode = XFER_UDMA_2; break;
  221. case 100: mode = XFER_MW_DMA_0; break;
  222. case 101: mode = XFER_MW_DMA_1; break;
  223. case 102: mode = XFER_MW_DMA_2; break;
  224. }
  225. if (mode != -1) {
  226. printk("SC1200: %s: changing (U)DMA mode\n", drive->name);
  227. hwif->dma_off_quietly(drive);
  228. if (ide_set_dma_mode(drive, mode) == 0)
  229. hwif->dma_host_on(drive);
  230. return;
  231. }
  232. sc1200_tunepio(drive, pio);
  233. }
  234. #ifdef CONFIG_PM
  235. static ide_hwif_t *lookup_pci_dev (ide_hwif_t *prev, struct pci_dev *dev)
  236. {
  237. int h;
  238. for (h = 0; h < MAX_HWIFS; h++) {
  239. ide_hwif_t *hwif = &ide_hwifs[h];
  240. if (prev) {
  241. if (hwif == prev)
  242. prev = NULL; // found previous, now look for next match
  243. } else {
  244. if (hwif && hwif->pci_dev == dev)
  245. return hwif; // found next match
  246. }
  247. }
  248. return NULL; // not found
  249. }
  250. typedef struct sc1200_saved_state_s {
  251. __u32 regs[4];
  252. } sc1200_saved_state_t;
  253. static int sc1200_suspend (struct pci_dev *dev, pm_message_t state)
  254. {
  255. ide_hwif_t *hwif = NULL;
  256. printk("SC1200: suspend(%u)\n", state.event);
  257. if (state.event == PM_EVENT_ON) {
  258. // we only save state when going from full power to less
  259. //
  260. // Loop over all interfaces that are part of this PCI device:
  261. //
  262. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  263. sc1200_saved_state_t *ss;
  264. unsigned int basereg, r;
  265. //
  266. // allocate a permanent save area, if not already allocated
  267. //
  268. ss = (sc1200_saved_state_t *)hwif->config_data;
  269. if (ss == NULL) {
  270. ss = kmalloc(sizeof(sc1200_saved_state_t), GFP_KERNEL);
  271. if (ss == NULL)
  272. return -ENOMEM;
  273. hwif->config_data = (unsigned long)ss;
  274. }
  275. ss = (sc1200_saved_state_t *)hwif->config_data;
  276. //
  277. // Save timing registers: this may be unnecessary if
  278. // BIOS also does it
  279. //
  280. basereg = hwif->channel ? 0x50 : 0x40;
  281. for (r = 0; r < 4; ++r) {
  282. pci_read_config_dword (hwif->pci_dev, basereg + (r<<2), &ss->regs[r]);
  283. }
  284. }
  285. }
  286. /* You don't need to iterate over disks -- sysfs should have done that for you already */
  287. pci_disable_device(dev);
  288. pci_set_power_state(dev, pci_choose_state(dev, state));
  289. return 0;
  290. }
  291. static int sc1200_resume (struct pci_dev *dev)
  292. {
  293. ide_hwif_t *hwif = NULL;
  294. int i;
  295. i = pci_enable_device(dev);
  296. if (i)
  297. return i;
  298. //
  299. // loop over all interfaces that are part of this pci device:
  300. //
  301. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  302. unsigned int basereg, r;
  303. sc1200_saved_state_t *ss = (sc1200_saved_state_t *)hwif->config_data;
  304. //
  305. // Restore timing registers: this may be unnecessary if BIOS also does it
  306. //
  307. basereg = hwif->channel ? 0x50 : 0x40;
  308. if (ss != NULL) {
  309. for (r = 0; r < 4; ++r) {
  310. pci_write_config_dword(hwif->pci_dev, basereg + (r<<2), ss->regs[r]);
  311. }
  312. }
  313. }
  314. return 0;
  315. }
  316. #endif
  317. /*
  318. * This gets invoked by the IDE driver once for each channel,
  319. * and performs channel-specific pre-initialization before drive probing.
  320. */
  321. static void __devinit init_hwif_sc1200 (ide_hwif_t *hwif)
  322. {
  323. hwif->set_pio_mode = &sc1200_set_pio_mode;
  324. hwif->set_dma_mode = &sc1200_set_dma_mode;
  325. if (hwif->dma_base == 0)
  326. return;
  327. hwif->udma_filter = sc1200_udma_filter;
  328. hwif->ide_dma_end = &sc1200_ide_dma_end;
  329. }
  330. static const struct ide_port_info sc1200_chipset __devinitdata = {
  331. .name = "SC1200",
  332. .init_hwif = init_hwif_sc1200,
  333. .host_flags = IDE_HFLAG_SERIALIZE |
  334. IDE_HFLAG_POST_SET_MODE |
  335. IDE_HFLAG_ABUSE_DMA_MODES |
  336. IDE_HFLAG_BOOTABLE,
  337. .pio_mask = ATA_PIO4,
  338. .mwdma_mask = ATA_MWDMA2,
  339. .udma_mask = ATA_UDMA2,
  340. };
  341. static int __devinit sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  342. {
  343. return ide_setup_pci_device(dev, &sc1200_chipset);
  344. }
  345. static const struct pci_device_id sc1200_pci_tbl[] = {
  346. { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SCx200_IDE), 0},
  347. { 0, },
  348. };
  349. MODULE_DEVICE_TABLE(pci, sc1200_pci_tbl);
  350. static struct pci_driver driver = {
  351. .name = "SC1200_IDE",
  352. .id_table = sc1200_pci_tbl,
  353. .probe = sc1200_init_one,
  354. #ifdef CONFIG_PM
  355. .suspend = sc1200_suspend,
  356. .resume = sc1200_resume,
  357. #endif
  358. };
  359. static int __init sc1200_ide_init(void)
  360. {
  361. return ide_pci_register_driver(&driver);
  362. }
  363. module_init(sc1200_ide_init);
  364. MODULE_AUTHOR("Mark Lord");
  365. MODULE_DESCRIPTION("PCI driver module for NS SC1200 IDE");
  366. MODULE_LICENSE("GPL");