sc1200.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_tune_dma(drive))
  220. return 0;
  221. return 1;
  222. }
  223. /* Replacement for the standard ide_dma_end action in
  224. * dma_proc.
  225. *
  226. * returns 1 on error, 0 otherwise
  227. */
  228. static int sc1200_ide_dma_end (ide_drive_t *drive)
  229. {
  230. ide_hwif_t *hwif = HWIF(drive);
  231. unsigned long dma_base = hwif->dma_base;
  232. byte dma_stat;
  233. dma_stat = inb(dma_base+2); /* get DMA status */
  234. if (!(dma_stat & 4))
  235. printk(" ide_dma_end dma_stat=%0x err=%x newerr=%x\n",
  236. dma_stat, ((dma_stat&7)!=4), ((dma_stat&2)==2));
  237. outb(dma_stat|0x1b, dma_base+2); /* clear the INTR & ERROR bits */
  238. outb(inb(dma_base)&~1, dma_base); /* !! DO THIS HERE !! stop DMA */
  239. drive->waiting_for_dma = 0;
  240. ide_destroy_dmatable(drive); /* purge DMA mappings */
  241. return (dma_stat & 7) != 4; /* verify good DMA status */
  242. }
  243. /*
  244. * sc1200_tuneproc() handles selection/setting of PIO modes
  245. * for both the chipset and drive.
  246. *
  247. * All existing BIOSs for this chipset guarantee that all drives
  248. * will have valid default PIO timings set up before we get here.
  249. */
  250. static void sc1200_tuneproc (ide_drive_t *drive, byte pio) /* mode=255 means "autotune" */
  251. {
  252. ide_hwif_t *hwif = HWIF(drive);
  253. int mode = -1;
  254. /*
  255. * bad abuse of ->tuneproc interface
  256. */
  257. switch (pio) {
  258. case 200: mode = XFER_UDMA_0; break;
  259. case 201: mode = XFER_UDMA_1; break;
  260. case 202: mode = XFER_UDMA_2; break;
  261. case 100: mode = XFER_MW_DMA_0; break;
  262. case 101: mode = XFER_MW_DMA_1; break;
  263. case 102: mode = XFER_MW_DMA_2; break;
  264. }
  265. if (mode != -1) {
  266. printk("SC1200: %s: changing (U)DMA mode\n", drive->name);
  267. hwif->dma_off_quietly(drive);
  268. if (sc1200_tune_chipset(drive, mode) == 0)
  269. hwif->dma_host_on(drive);
  270. return;
  271. }
  272. pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
  273. printk("SC1200: %s: setting PIO mode%d\n", drive->name, pio);
  274. if (sc1200_set_xfer_mode(drive, XFER_PIO_0 + pio) == 0)
  275. sc1200_tunepio(drive, pio);
  276. }
  277. #ifdef CONFIG_PM
  278. static ide_hwif_t *lookup_pci_dev (ide_hwif_t *prev, struct pci_dev *dev)
  279. {
  280. int h;
  281. for (h = 0; h < MAX_HWIFS; h++) {
  282. ide_hwif_t *hwif = &ide_hwifs[h];
  283. if (prev) {
  284. if (hwif == prev)
  285. prev = NULL; // found previous, now look for next match
  286. } else {
  287. if (hwif && hwif->pci_dev == dev)
  288. return hwif; // found next match
  289. }
  290. }
  291. return NULL; // not found
  292. }
  293. typedef struct sc1200_saved_state_s {
  294. __u32 regs[4];
  295. } sc1200_saved_state_t;
  296. static int sc1200_suspend (struct pci_dev *dev, pm_message_t state)
  297. {
  298. ide_hwif_t *hwif = NULL;
  299. printk("SC1200: suspend(%u)\n", state.event);
  300. if (state.event == PM_EVENT_ON) {
  301. // we only save state when going from full power to less
  302. //
  303. // Loop over all interfaces that are part of this PCI device:
  304. //
  305. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  306. sc1200_saved_state_t *ss;
  307. unsigned int basereg, r;
  308. //
  309. // allocate a permanent save area, if not already allocated
  310. //
  311. ss = (sc1200_saved_state_t *)hwif->config_data;
  312. if (ss == NULL) {
  313. ss = kmalloc(sizeof(sc1200_saved_state_t), GFP_KERNEL);
  314. if (ss == NULL)
  315. return -ENOMEM;
  316. hwif->config_data = (unsigned long)ss;
  317. }
  318. ss = (sc1200_saved_state_t *)hwif->config_data;
  319. //
  320. // Save timing registers: this may be unnecessary if
  321. // BIOS also does it
  322. //
  323. basereg = hwif->channel ? 0x50 : 0x40;
  324. for (r = 0; r < 4; ++r) {
  325. pci_read_config_dword (hwif->pci_dev, basereg + (r<<2), &ss->regs[r]);
  326. }
  327. }
  328. }
  329. /* You don't need to iterate over disks -- sysfs should have done that for you already */
  330. pci_disable_device(dev);
  331. pci_set_power_state(dev, pci_choose_state(dev, state));
  332. dev->current_state = state.event;
  333. return 0;
  334. }
  335. static int sc1200_resume (struct pci_dev *dev)
  336. {
  337. ide_hwif_t *hwif = NULL;
  338. pci_set_power_state(dev, PCI_D0); // bring chip back from sleep state
  339. dev->current_state = PM_EVENT_ON;
  340. pci_enable_device(dev);
  341. //
  342. // loop over all interfaces that are part of this pci device:
  343. //
  344. while ((hwif = lookup_pci_dev(hwif, dev)) != NULL) {
  345. unsigned int basereg, r, d, format;
  346. sc1200_saved_state_t *ss = (sc1200_saved_state_t *)hwif->config_data;
  347. //
  348. // Restore timing registers: this may be unnecessary if BIOS also does it
  349. //
  350. basereg = hwif->channel ? 0x50 : 0x40;
  351. if (ss != NULL) {
  352. for (r = 0; r < 4; ++r) {
  353. pci_write_config_dword(hwif->pci_dev, basereg + (r<<2), ss->regs[r]);
  354. }
  355. }
  356. //
  357. // Re-program drive PIO modes
  358. //
  359. pci_read_config_dword(hwif->pci_dev, basereg+4, &format);
  360. format = (format >> 31) & 1;
  361. if (format)
  362. format += sc1200_get_pci_clock();
  363. for (d = 0; d < 2; ++d) {
  364. ide_drive_t *drive = &(hwif->drives[d]);
  365. if (drive->present) {
  366. unsigned int pio, timings;
  367. pci_read_config_dword(hwif->pci_dev, basereg+(drive->select.b.unit << 3), &timings);
  368. for (pio = 0; pio <= 4; ++pio) {
  369. if (sc1200_pio_timings[format][pio] == timings)
  370. break;
  371. }
  372. if (pio > 4)
  373. pio = 255; /* autotune */
  374. (void)sc1200_tuneproc(drive, pio);
  375. }
  376. }
  377. //
  378. // Re-program drive DMA modes
  379. //
  380. for (d = 0; d < MAX_DRIVES; ++d) {
  381. ide_drive_t *drive = &(hwif->drives[d]);
  382. if (drive->present && !__ide_dma_bad_drive(drive)) {
  383. int enable_dma = drive->using_dma;
  384. hwif->dma_off_quietly(drive);
  385. if (sc1200_config_dma(drive))
  386. enable_dma = 0;
  387. if (enable_dma)
  388. hwif->dma_host_on(drive);
  389. }
  390. }
  391. }
  392. return 0;
  393. }
  394. #endif
  395. /*
  396. * This gets invoked by the IDE driver once for each channel,
  397. * and performs channel-specific pre-initialization before drive probing.
  398. */
  399. static void __devinit init_hwif_sc1200 (ide_hwif_t *hwif)
  400. {
  401. if (hwif->mate)
  402. hwif->serialized = hwif->mate->serialized = 1;
  403. hwif->autodma = 0;
  404. if (hwif->dma_base) {
  405. hwif->udma_filter = sc1200_udma_filter;
  406. hwif->ide_dma_check = &sc1200_config_dma;
  407. hwif->ide_dma_end = &sc1200_ide_dma_end;
  408. if (!noautodma)
  409. hwif->autodma = 1;
  410. hwif->tuneproc = &sc1200_tuneproc;
  411. hwif->speedproc = &sc1200_tune_chipset;
  412. }
  413. hwif->atapi_dma = 1;
  414. hwif->ultra_mask = 0x07;
  415. hwif->mwdma_mask = 0x07;
  416. hwif->drives[0].autodma = hwif->autodma;
  417. hwif->drives[1].autodma = hwif->autodma;
  418. }
  419. static ide_pci_device_t sc1200_chipset __devinitdata = {
  420. .name = "SC1200",
  421. .init_hwif = init_hwif_sc1200,
  422. .channels = 2,
  423. .autodma = AUTODMA,
  424. .bootable = ON_BOARD,
  425. };
  426. static int __devinit sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  427. {
  428. return ide_setup_pci_device(dev, &sc1200_chipset);
  429. }
  430. static struct pci_device_id sc1200_pci_tbl[] = {
  431. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_IDE), 0},
  432. { 0, },
  433. };
  434. MODULE_DEVICE_TABLE(pci, sc1200_pci_tbl);
  435. static struct pci_driver driver = {
  436. .name = "SC1200_IDE",
  437. .id_table = sc1200_pci_tbl,
  438. .probe = sc1200_init_one,
  439. #ifdef CONFIG_PM
  440. .suspend = sc1200_suspend,
  441. .resume = sc1200_resume,
  442. #endif
  443. };
  444. static int __init sc1200_ide_init(void)
  445. {
  446. return ide_pci_register_driver(&driver);
  447. }
  448. module_init(sc1200_ide_init);
  449. MODULE_AUTHOR("Mark Lord");
  450. MODULE_DESCRIPTION("PCI driver module for NS SC1200 IDE");
  451. MODULE_LICENSE("GPL");