sc1200.c 14 KB

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