sc1200.c 15 KB

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