sc1200.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * linux/drivers/ide/pci/sc1200.c Version 0.94 Mar 10 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. extern char *ide_xfer_verbose (byte xfer_rate);
  65. /*
  66. * Set a new transfer mode at the drive
  67. */
  68. static int sc1200_set_xfer_mode (ide_drive_t *drive, byte mode)
  69. {
  70. printk("%s: sc1200_set_xfer_mode(%s)\n", drive->name, ide_xfer_verbose(mode));
  71. return ide_config_drive_speed(drive, mode);
  72. }
  73. /*
  74. * Here are the standard PIO mode 0-4 timings for each "format".
  75. * Format-0 uses fast data reg timings, with slower command reg timings.
  76. * Format-1 uses fast timings for all registers, but won't work with all drives.
  77. */
  78. static const unsigned int sc1200_pio_timings[4][5] =
  79. {{0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010}, // format0 33Mhz
  80. {0xd1329172, 0x71212171, 0x30200080, 0x20102010, 0x00100010}, // format1, 33Mhz
  81. {0xfaa3f4f3, 0xc23232b2, 0x513101c1, 0x31213121, 0x10211021}, // format1, 48Mhz
  82. {0xfff4fff4, 0xf35353d3, 0x814102f1, 0x42314231, 0x11311131}}; // format1, 66Mhz
  83. /*
  84. * After chip reset, the PIO timings are set to 0x00009172, which is not valid.
  85. */
  86. //#define SC1200_BAD_PIO(timings) (((timings)&~0x80000000)==0x00009172)
  87. static void sc1200_tunepio(ide_drive_t *drive, u8 pio)
  88. {
  89. ide_hwif_t *hwif = drive->hwif;
  90. struct pci_dev *pdev = hwif->pci_dev;
  91. unsigned int basereg = hwif->channel ? 0x50 : 0x40, format = 0;
  92. pci_read_config_dword(pdev, basereg + 4, &format);
  93. format = (format >> 31) & 1;
  94. if (format)
  95. format += sc1200_get_pci_clock();
  96. pci_write_config_dword(pdev, basereg + ((drive->dn & 1) << 3),
  97. sc1200_pio_timings[format][pio]);
  98. }
  99. /*
  100. * The SC1200 specifies that two drives sharing a cable cannot mix
  101. * UDMA/MDMA. It has to be one or the other, for the pair, though
  102. * different timings can still be chosen for each drive. We could
  103. * set the appropriate timing bits on the fly, but that might be
  104. * a bit confusing. So, for now we statically handle this requirement
  105. * by looking at our mate drive to see what it is capable of, before
  106. * choosing a mode for our own drive.
  107. */
  108. static u8 sc1200_udma_filter(ide_drive_t *drive)
  109. {
  110. ide_hwif_t *hwif = drive->hwif;
  111. ide_drive_t *mate = &hwif->drives[(drive->dn & 1) ^ 1];
  112. struct hd_driveid *mateid = mate->id;
  113. u8 mask = hwif->ultra_mask;
  114. if (mate->present == 0)
  115. goto out;
  116. if ((mateid->capability & 1) && __ide_dma_bad_drive(mate) == 0) {
  117. if ((mateid->field_valid & 4) && (mateid->dma_ultra & 7))
  118. goto out;
  119. if ((mateid->field_valid & 2) && (mateid->dma_mword & 7))
  120. mask = 0;
  121. }
  122. out:
  123. return mask;
  124. }
  125. static int sc1200_tune_chipset(ide_drive_t *drive, u8 mode)
  126. {
  127. ide_hwif_t *hwif = HWIF(drive);
  128. int unit = drive->select.b.unit;
  129. unsigned int reg, timings;
  130. unsigned short pci_clock;
  131. unsigned int basereg = hwif->channel ? 0x50 : 0x40;
  132. mode = ide_rate_filter(drive, mode);
  133. /*
  134. * Tell the drive to switch to the new mode; abort on failure.
  135. */
  136. if (sc1200_set_xfer_mode(drive, mode)) {
  137. printk("SC1200: set xfer mode failure\n");
  138. return 1; /* failure */
  139. }
  140. switch (mode) {
  141. case XFER_PIO_4:
  142. case XFER_PIO_3:
  143. case XFER_PIO_2:
  144. case XFER_PIO_1:
  145. case XFER_PIO_0:
  146. sc1200_tunepio(drive, mode - XFER_PIO_0);
  147. return 0;
  148. }
  149. pci_clock = sc1200_get_pci_clock();
  150. /*
  151. * Now tune the chipset to match the drive:
  152. *
  153. * Note that each DMA mode has several timings associated with it.
  154. * The correct timing depends on the fast PCI clock freq.
  155. */
  156. timings = 0;
  157. switch (mode) {
  158. case XFER_UDMA_0:
  159. switch (pci_clock) {
  160. case PCI_CLK_33: timings = 0x00921250; break;
  161. case PCI_CLK_48: timings = 0x00932470; break;
  162. case PCI_CLK_66: timings = 0x009436a1; break;
  163. }
  164. break;
  165. case XFER_UDMA_1:
  166. switch (pci_clock) {
  167. case PCI_CLK_33: timings = 0x00911140; break;
  168. case PCI_CLK_48: timings = 0x00922260; break;
  169. case PCI_CLK_66: timings = 0x00933481; break;
  170. }
  171. break;
  172. case XFER_UDMA_2:
  173. switch (pci_clock) {
  174. case PCI_CLK_33: timings = 0x00911030; break;
  175. case PCI_CLK_48: timings = 0x00922140; break;
  176. case PCI_CLK_66: timings = 0x00923261; break;
  177. }
  178. break;
  179. case XFER_MW_DMA_0:
  180. switch (pci_clock) {
  181. case PCI_CLK_33: timings = 0x00077771; break;
  182. case PCI_CLK_48: timings = 0x000bbbb2; break;
  183. case PCI_CLK_66: timings = 0x000ffff3; break;
  184. }
  185. break;
  186. case XFER_MW_DMA_1:
  187. switch (pci_clock) {
  188. case PCI_CLK_33: timings = 0x00012121; break;
  189. case PCI_CLK_48: timings = 0x00024241; break;
  190. case PCI_CLK_66: timings = 0x00035352; break;
  191. }
  192. break;
  193. case XFER_MW_DMA_2:
  194. switch (pci_clock) {
  195. case PCI_CLK_33: timings = 0x00002020; break;
  196. case PCI_CLK_48: timings = 0x00013131; break;
  197. case PCI_CLK_66: timings = 0x00015151; break;
  198. }
  199. break;
  200. default:
  201. BUG();
  202. break;
  203. }
  204. if (unit == 0) { /* are we configuring drive0? */
  205. pci_read_config_dword(hwif->pci_dev, basereg+4, &reg);
  206. timings |= reg & 0x80000000; /* preserve PIO format bit */
  207. pci_write_config_dword(hwif->pci_dev, basereg+4, timings);
  208. } else {
  209. pci_write_config_dword(hwif->pci_dev, basereg+12, timings);
  210. }
  211. return 0; /* success */
  212. }
  213. /*
  214. * sc1200_config_dma() handles selection/setting of DMA/UDMA modes
  215. * for both the chipset and drive.
  216. */
  217. static int sc1200_config_dma (ide_drive_t *drive)
  218. {
  219. if (ide_use_dma(drive)) {
  220. u8 mode = ide_max_dma_mode(drive);
  221. if (mode && drive->hwif->speedproc(drive, mode) == 0)
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. /* Replacement for the standard ide_dma_end action in
  227. * dma_proc.
  228. *
  229. * returns 1 on error, 0 otherwise
  230. */
  231. static int sc1200_ide_dma_end (ide_drive_t *drive)
  232. {
  233. ide_hwif_t *hwif = HWIF(drive);
  234. unsigned long dma_base = hwif->dma_base;
  235. byte dma_stat;
  236. dma_stat = inb(dma_base+2); /* get DMA status */
  237. if (!(dma_stat & 4))
  238. printk(" ide_dma_end dma_stat=%0x err=%x newerr=%x\n",
  239. dma_stat, ((dma_stat&7)!=4), ((dma_stat&2)==2));
  240. outb(dma_stat|0x1b, dma_base+2); /* clear the INTR & ERROR bits */
  241. outb(inb(dma_base)&~1, dma_base); /* !! DO THIS HERE !! stop DMA */
  242. drive->waiting_for_dma = 0;
  243. ide_destroy_dmatable(drive); /* purge DMA mappings */
  244. return (dma_stat & 7) != 4; /* verify good DMA status */
  245. }
  246. /*
  247. * sc1200_tuneproc() handles selection/setting of PIO modes
  248. * for both the chipset and drive.
  249. *
  250. * All existing BIOSs for this chipset guarantee that all drives
  251. * will have valid default PIO timings set up before we get here.
  252. */
  253. static void sc1200_tuneproc (ide_drive_t *drive, byte pio) /* mode=255 means "autotune" */
  254. {
  255. ide_hwif_t *hwif = HWIF(drive);
  256. int mode = -1;
  257. /*
  258. * bad abuse of ->tuneproc interface
  259. */
  260. switch (pio) {
  261. case 200: mode = XFER_UDMA_0; break;
  262. case 201: mode = XFER_UDMA_1; break;
  263. case 202: mode = XFER_UDMA_2; break;
  264. case 100: mode = XFER_MW_DMA_0; break;
  265. case 101: mode = XFER_MW_DMA_1; break;
  266. case 102: mode = XFER_MW_DMA_2; break;
  267. }
  268. if (mode != -1) {
  269. printk("SC1200: %s: changing (U)DMA mode\n", drive->name);
  270. hwif->dma_off_quietly(drive);
  271. if (sc1200_tune_chipset(drive, mode) == 0)
  272. hwif->dma_host_on(drive);
  273. return;
  274. }
  275. pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
  276. printk("SC1200: %s: setting PIO mode%d\n", drive->name, pio);
  277. if (sc1200_set_xfer_mode(drive, XFER_PIO_0 + pio) == 0)
  278. sc1200_tunepio(drive, pio);
  279. }
  280. #ifdef CONFIG_PM
  281. static ide_hwif_t *lookup_pci_dev (ide_hwif_t *prev, struct pci_dev *dev)
  282. {
  283. int h;
  284. for (h = 0; h < MAX_HWIFS; h++) {
  285. ide_hwif_t *hwif = &ide_hwifs[h];
  286. if (prev) {
  287. if (hwif == prev)
  288. prev = NULL; // found previous, now look for next match
  289. } else {
  290. if (hwif && hwif->pci_dev == dev)
  291. return hwif; // found next match
  292. }
  293. }
  294. return NULL; // not found
  295. }
  296. typedef struct sc1200_saved_state_s {
  297. __u32 regs[4];
  298. } sc1200_saved_state_t;
  299. static int sc1200_suspend (struct pci_dev *dev, pm_message_t state)
  300. {
  301. ide_hwif_t *hwif = NULL;
  302. printk("SC1200: suspend(%u)\n", state.event);
  303. if (state.event == PM_EVENT_ON) {
  304. // we only save state when going from full power to less
  305. //
  306. // Loop over all interfaces that are part of this PCI device:
  307. //
  308. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  309. sc1200_saved_state_t *ss;
  310. unsigned int basereg, r;
  311. //
  312. // allocate a permanent save area, if not already allocated
  313. //
  314. ss = (sc1200_saved_state_t *)hwif->config_data;
  315. if (ss == NULL) {
  316. ss = kmalloc(sizeof(sc1200_saved_state_t), GFP_KERNEL);
  317. if (ss == NULL)
  318. return -ENOMEM;
  319. hwif->config_data = (unsigned long)ss;
  320. }
  321. ss = (sc1200_saved_state_t *)hwif->config_data;
  322. //
  323. // Save timing registers: this may be unnecessary if
  324. // BIOS also does it
  325. //
  326. basereg = hwif->channel ? 0x50 : 0x40;
  327. for (r = 0; r < 4; ++r) {
  328. pci_read_config_dword (hwif->pci_dev, basereg + (r<<2), &ss->regs[r]);
  329. }
  330. }
  331. }
  332. /* You don't need to iterate over disks -- sysfs should have done that for you already */
  333. pci_disable_device(dev);
  334. pci_set_power_state(dev, pci_choose_state(dev, state));
  335. dev->current_state = state.event;
  336. return 0;
  337. }
  338. static int sc1200_resume (struct pci_dev *dev)
  339. {
  340. ide_hwif_t *hwif = NULL;
  341. pci_set_power_state(dev, PCI_D0); // bring chip back from sleep state
  342. dev->current_state = PM_EVENT_ON;
  343. pci_enable_device(dev);
  344. //
  345. // loop over all interfaces that are part of this pci device:
  346. //
  347. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  348. unsigned int basereg, r, d, format;
  349. sc1200_saved_state_t *ss = (sc1200_saved_state_t *)hwif->config_data;
  350. //
  351. // Restore timing registers: this may be unnecessary if BIOS also does it
  352. //
  353. basereg = hwif->channel ? 0x50 : 0x40;
  354. if (ss != NULL) {
  355. for (r = 0; r < 4; ++r) {
  356. pci_write_config_dword(hwif->pci_dev, basereg + (r<<2), ss->regs[r]);
  357. }
  358. }
  359. //
  360. // Re-program drive PIO modes
  361. //
  362. pci_read_config_dword(hwif->pci_dev, basereg+4, &format);
  363. format = (format >> 31) & 1;
  364. if (format)
  365. format += sc1200_get_pci_clock();
  366. for (d = 0; d < 2; ++d) {
  367. ide_drive_t *drive = &(hwif->drives[d]);
  368. if (drive->present) {
  369. unsigned int pio, timings;
  370. pci_read_config_dword(hwif->pci_dev, basereg+(drive->select.b.unit << 3), &timings);
  371. for (pio = 0; pio <= 4; ++pio) {
  372. if (sc1200_pio_timings[format][pio] == timings)
  373. break;
  374. }
  375. if (pio > 4)
  376. pio = 255; /* autotune */
  377. (void)sc1200_tuneproc(drive, pio);
  378. }
  379. }
  380. //
  381. // Re-program drive DMA modes
  382. //
  383. for (d = 0; d < MAX_DRIVES; ++d) {
  384. ide_drive_t *drive = &(hwif->drives[d]);
  385. if (drive->present && !__ide_dma_bad_drive(drive)) {
  386. int enable_dma = drive->using_dma;
  387. hwif->dma_off_quietly(drive);
  388. if (sc1200_config_dma(drive))
  389. enable_dma = 0;
  390. if (enable_dma)
  391. hwif->dma_host_on(drive);
  392. }
  393. }
  394. }
  395. return 0;
  396. }
  397. #endif
  398. /*
  399. * This gets invoked by the IDE driver once for each channel,
  400. * and performs channel-specific pre-initialization before drive probing.
  401. */
  402. static void __devinit init_hwif_sc1200 (ide_hwif_t *hwif)
  403. {
  404. if (hwif->mate)
  405. hwif->serialized = hwif->mate->serialized = 1;
  406. hwif->autodma = 0;
  407. if (hwif->dma_base) {
  408. hwif->udma_filter = sc1200_udma_filter;
  409. hwif->ide_dma_check = &sc1200_config_dma;
  410. hwif->ide_dma_end = &sc1200_ide_dma_end;
  411. if (!noautodma)
  412. hwif->autodma = 1;
  413. hwif->tuneproc = &sc1200_tuneproc;
  414. hwif->speedproc = &sc1200_tune_chipset;
  415. }
  416. hwif->atapi_dma = 1;
  417. hwif->ultra_mask = 0x07;
  418. hwif->mwdma_mask = 0x07;
  419. hwif->drives[0].autodma = hwif->autodma;
  420. hwif->drives[1].autodma = hwif->autodma;
  421. }
  422. static ide_pci_device_t sc1200_chipset __devinitdata = {
  423. .name = "SC1200",
  424. .init_hwif = init_hwif_sc1200,
  425. .channels = 2,
  426. .autodma = AUTODMA,
  427. .bootable = ON_BOARD,
  428. };
  429. static int __devinit sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  430. {
  431. return ide_setup_pci_device(dev, &sc1200_chipset);
  432. }
  433. static struct pci_device_id sc1200_pci_tbl[] = {
  434. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_IDE), 0},
  435. { 0, },
  436. };
  437. MODULE_DEVICE_TABLE(pci, sc1200_pci_tbl);
  438. static struct pci_driver driver = {
  439. .name = "SC1200_IDE",
  440. .id_table = sc1200_pci_tbl,
  441. .probe = sc1200_init_one,
  442. #ifdef CONFIG_PM
  443. .suspend = sc1200_suspend,
  444. .resume = sc1200_resume,
  445. #endif
  446. };
  447. static int __init sc1200_ide_init(void)
  448. {
  449. return ide_pci_register_driver(&driver);
  450. }
  451. module_init(sc1200_ide_init);
  452. MODULE_AUTHOR("Mark Lord");
  453. MODULE_DESCRIPTION("PCI driver module for NS SC1200 IDE");
  454. MODULE_LICENSE("GPL");