sc1200.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * linux/drivers/ide/pci/sc1200.c Version 0.95 Jun 16 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. BUG();
  174. break;
  175. }
  176. if (unit == 0) { /* are we configuring drive0? */
  177. pci_read_config_dword(hwif->pci_dev, basereg+4, &reg);
  178. timings |= reg & 0x80000000; /* preserve PIO format bit */
  179. pci_write_config_dword(hwif->pci_dev, basereg+4, timings);
  180. } else {
  181. pci_write_config_dword(hwif->pci_dev, basereg+12, timings);
  182. }
  183. }
  184. /*
  185. * sc1200_config_dma() handles selection/setting of DMA/UDMA modes
  186. * for both the chipset and drive.
  187. */
  188. static int sc1200_config_dma (ide_drive_t *drive)
  189. {
  190. if (ide_tune_dma(drive))
  191. return 0;
  192. return 1;
  193. }
  194. /* Replacement for the standard ide_dma_end action in
  195. * dma_proc.
  196. *
  197. * returns 1 on error, 0 otherwise
  198. */
  199. static int sc1200_ide_dma_end (ide_drive_t *drive)
  200. {
  201. ide_hwif_t *hwif = HWIF(drive);
  202. unsigned long dma_base = hwif->dma_base;
  203. byte dma_stat;
  204. dma_stat = inb(dma_base+2); /* get DMA status */
  205. if (!(dma_stat & 4))
  206. printk(" ide_dma_end dma_stat=%0x err=%x newerr=%x\n",
  207. dma_stat, ((dma_stat&7)!=4), ((dma_stat&2)==2));
  208. outb(dma_stat|0x1b, dma_base+2); /* clear the INTR & ERROR bits */
  209. outb(inb(dma_base)&~1, dma_base); /* !! DO THIS HERE !! stop DMA */
  210. drive->waiting_for_dma = 0;
  211. ide_destroy_dmatable(drive); /* purge DMA mappings */
  212. return (dma_stat & 7) != 4; /* verify good DMA status */
  213. }
  214. /*
  215. * sc1200_set_pio_mode() handles setting of PIO modes
  216. * for both the chipset and drive.
  217. *
  218. * All existing BIOSs for this chipset guarantee that all drives
  219. * will have valid default PIO timings set up before we get here.
  220. */
  221. static void sc1200_set_pio_mode(ide_drive_t *drive, const u8 pio)
  222. {
  223. ide_hwif_t *hwif = HWIF(drive);
  224. int mode = -1;
  225. /*
  226. * bad abuse of ->set_pio_mode interface
  227. */
  228. switch (pio) {
  229. case 200: mode = XFER_UDMA_0; break;
  230. case 201: mode = XFER_UDMA_1; break;
  231. case 202: mode = XFER_UDMA_2; break;
  232. case 100: mode = XFER_MW_DMA_0; break;
  233. case 101: mode = XFER_MW_DMA_1; break;
  234. case 102: mode = XFER_MW_DMA_2; break;
  235. }
  236. if (mode != -1) {
  237. printk("SC1200: %s: changing (U)DMA mode\n", drive->name);
  238. hwif->dma_off_quietly(drive);
  239. if (ide_set_dma_mode(drive, mode) == 0)
  240. hwif->dma_host_on(drive);
  241. return;
  242. }
  243. sc1200_tunepio(drive, pio);
  244. }
  245. #ifdef CONFIG_PM
  246. static ide_hwif_t *lookup_pci_dev (ide_hwif_t *prev, struct pci_dev *dev)
  247. {
  248. int h;
  249. for (h = 0; h < MAX_HWIFS; h++) {
  250. ide_hwif_t *hwif = &ide_hwifs[h];
  251. if (prev) {
  252. if (hwif == prev)
  253. prev = NULL; // found previous, now look for next match
  254. } else {
  255. if (hwif && hwif->pci_dev == dev)
  256. return hwif; // found next match
  257. }
  258. }
  259. return NULL; // not found
  260. }
  261. typedef struct sc1200_saved_state_s {
  262. __u32 regs[4];
  263. } sc1200_saved_state_t;
  264. static int sc1200_suspend (struct pci_dev *dev, pm_message_t state)
  265. {
  266. ide_hwif_t *hwif = NULL;
  267. printk("SC1200: suspend(%u)\n", state.event);
  268. if (state.event == PM_EVENT_ON) {
  269. // we only save state when going from full power to less
  270. //
  271. // Loop over all interfaces that are part of this PCI device:
  272. //
  273. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  274. sc1200_saved_state_t *ss;
  275. unsigned int basereg, r;
  276. //
  277. // allocate a permanent save area, if not already allocated
  278. //
  279. ss = (sc1200_saved_state_t *)hwif->config_data;
  280. if (ss == NULL) {
  281. ss = kmalloc(sizeof(sc1200_saved_state_t), GFP_KERNEL);
  282. if (ss == NULL)
  283. return -ENOMEM;
  284. hwif->config_data = (unsigned long)ss;
  285. }
  286. ss = (sc1200_saved_state_t *)hwif->config_data;
  287. //
  288. // Save timing registers: this may be unnecessary if
  289. // BIOS also does it
  290. //
  291. basereg = hwif->channel ? 0x50 : 0x40;
  292. for (r = 0; r < 4; ++r) {
  293. pci_read_config_dword (hwif->pci_dev, basereg + (r<<2), &ss->regs[r]);
  294. }
  295. }
  296. }
  297. /* You don't need to iterate over disks -- sysfs should have done that for you already */
  298. pci_disable_device(dev);
  299. pci_set_power_state(dev, pci_choose_state(dev, state));
  300. dev->current_state = state.event;
  301. return 0;
  302. }
  303. static int sc1200_resume (struct pci_dev *dev)
  304. {
  305. ide_hwif_t *hwif = NULL;
  306. pci_set_power_state(dev, PCI_D0); // bring chip back from sleep state
  307. dev->current_state = PM_EVENT_ON;
  308. pci_enable_device(dev);
  309. //
  310. // loop over all interfaces that are part of this pci device:
  311. //
  312. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  313. unsigned int basereg, r;
  314. sc1200_saved_state_t *ss = (sc1200_saved_state_t *)hwif->config_data;
  315. //
  316. // Restore timing registers: this may be unnecessary if BIOS also does it
  317. //
  318. basereg = hwif->channel ? 0x50 : 0x40;
  319. if (ss != NULL) {
  320. for (r = 0; r < 4; ++r) {
  321. pci_write_config_dword(hwif->pci_dev, basereg + (r<<2), ss->regs[r]);
  322. }
  323. }
  324. }
  325. return 0;
  326. }
  327. #endif
  328. /*
  329. * This gets invoked by the IDE driver once for each channel,
  330. * and performs channel-specific pre-initialization before drive probing.
  331. */
  332. static void __devinit init_hwif_sc1200 (ide_hwif_t *hwif)
  333. {
  334. if (hwif->mate)
  335. hwif->serialized = hwif->mate->serialized = 1;
  336. hwif->autodma = 0;
  337. hwif->set_pio_mode = &sc1200_set_pio_mode;
  338. hwif->set_dma_mode = &sc1200_set_dma_mode;
  339. if (hwif->dma_base == 0)
  340. return;
  341. hwif->udma_filter = sc1200_udma_filter;
  342. hwif->ide_dma_check = &sc1200_config_dma;
  343. hwif->ide_dma_end = &sc1200_ide_dma_end;
  344. if (!noautodma)
  345. hwif->autodma = 1;
  346. hwif->atapi_dma = 1;
  347. hwif->ultra_mask = 0x07;
  348. hwif->mwdma_mask = 0x07;
  349. hwif->drives[0].autodma = hwif->autodma;
  350. hwif->drives[1].autodma = hwif->autodma;
  351. }
  352. static ide_pci_device_t sc1200_chipset __devinitdata = {
  353. .name = "SC1200",
  354. .init_hwif = init_hwif_sc1200,
  355. .autodma = AUTODMA,
  356. .bootable = ON_BOARD,
  357. .host_flags = IDE_HFLAG_ABUSE_DMA_MODES | IDE_HFLAG_POST_SET_MODE,
  358. .pio_mask = ATA_PIO4,
  359. };
  360. static int __devinit sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  361. {
  362. return ide_setup_pci_device(dev, &sc1200_chipset);
  363. }
  364. static struct pci_device_id sc1200_pci_tbl[] = {
  365. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_IDE), 0},
  366. { 0, },
  367. };
  368. MODULE_DEVICE_TABLE(pci, sc1200_pci_tbl);
  369. static struct pci_driver driver = {
  370. .name = "SC1200_IDE",
  371. .id_table = sc1200_pci_tbl,
  372. .probe = sc1200_init_one,
  373. #ifdef CONFIG_PM
  374. .suspend = sc1200_suspend,
  375. .resume = sc1200_resume,
  376. #endif
  377. };
  378. static int __init sc1200_ide_init(void)
  379. {
  380. return ide_pci_register_driver(&driver);
  381. }
  382. module_init(sc1200_ide_init);
  383. MODULE_AUTHOR("Mark Lord");
  384. MODULE_DESCRIPTION("PCI driver module for NS SC1200 IDE");
  385. MODULE_LICENSE("GPL");