sc1200.c 14 KB

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