sis5513.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * linux/drivers/ide/pci/sis5513.c Version 0.16ac+vp Jun 18, 2003
  3. *
  4. * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  5. * Copyright (C) 2002 Lionel Bouton <Lionel.Bouton@inet6.fr>, Maintainer
  6. * Copyright (C) 2003 Vojtech Pavlik <vojtech@suse.cz>
  7. * May be copied or modified under the terms of the GNU General Public License
  8. *
  9. *
  10. * Thanks :
  11. *
  12. * SiS Taiwan : for direct support and hardware.
  13. * Daniela Engert : for initial ATA100 advices and numerous others.
  14. * John Fremlin, Manfred Spraul, Dave Morgan, Peter Kjellerstedt :
  15. * for checking code correctness, providing patches.
  16. *
  17. *
  18. * Original tests and design on the SiS620 chipset.
  19. * ATA100 tests and design on the SiS735 chipset.
  20. * ATA16/33 support from specs
  21. * ATA133 support for SiS961/962 by L.C. Chang <lcchang@sis.com.tw>
  22. * ATA133 961/962/963 fixes by Vojtech Pavlik <vojtech@suse.cz>
  23. *
  24. * Documentation:
  25. * SiS chipset documentation available under NDA to companies only
  26. * (not to individuals).
  27. */
  28. /*
  29. * The original SiS5513 comes from a SiS5511/55112/5513 chipset. The original
  30. * SiS5513 was also used in the SiS5596/5513 chipset. Thus if we see a SiS5511
  31. * or SiS5596, we can assume we see the first MWDMA-16 capable SiS5513 chip.
  32. *
  33. * Later SiS chipsets integrated the 5513 functionality into the NorthBridge,
  34. * starting with SiS5571 and up to SiS745. The PCI ID didn't change, though. We
  35. * can figure out that we have a more modern and more capable 5513 by looking
  36. * for the respective NorthBridge IDs.
  37. *
  38. * Even later (96x family) SiS chipsets use the MuTIOL link and place the 5513
  39. * into the SouthBrige. Here we cannot rely on looking up the NorthBridge PCI
  40. * ID, while the now ATA-133 capable 5513 still has the same PCI ID.
  41. * Fortunately the 5513 can be 'unmasked' by fiddling with some config space
  42. * bits, changing its device id to the true one - 5517 for 961 and 5518 for
  43. * 962/963.
  44. */
  45. #include <linux/config.h>
  46. #include <linux/types.h>
  47. #include <linux/module.h>
  48. #include <linux/kernel.h>
  49. #include <linux/delay.h>
  50. #include <linux/timer.h>
  51. #include <linux/mm.h>
  52. #include <linux/ioport.h>
  53. #include <linux/blkdev.h>
  54. #include <linux/hdreg.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/pci.h>
  57. #include <linux/init.h>
  58. #include <linux/ide.h>
  59. #include <asm/irq.h>
  60. #include "ide-timing.h"
  61. #define DISPLAY_SIS_TIMINGS
  62. /* registers layout and init values are chipset family dependant */
  63. #define ATA_16 0x01
  64. #define ATA_33 0x02
  65. #define ATA_66 0x03
  66. #define ATA_100a 0x04 // SiS730/SiS550 is ATA100 with ATA66 layout
  67. #define ATA_100 0x05
  68. #define ATA_133a 0x06 // SiS961b with 133 support
  69. #define ATA_133 0x07 // SiS962/963
  70. static u8 chipset_family;
  71. /*
  72. * Devices supported
  73. */
  74. static const struct {
  75. const char *name;
  76. u16 host_id;
  77. u8 chipset_family;
  78. u8 flags;
  79. } SiSHostChipInfo[] = {
  80. { "SiS965", PCI_DEVICE_ID_SI_965, ATA_133 },
  81. { "SiS745", PCI_DEVICE_ID_SI_745, ATA_100 },
  82. { "SiS735", PCI_DEVICE_ID_SI_735, ATA_100 },
  83. { "SiS733", PCI_DEVICE_ID_SI_733, ATA_100 },
  84. { "SiS635", PCI_DEVICE_ID_SI_635, ATA_100 },
  85. { "SiS633", PCI_DEVICE_ID_SI_633, ATA_100 },
  86. { "SiS730", PCI_DEVICE_ID_SI_730, ATA_100a },
  87. { "SiS550", PCI_DEVICE_ID_SI_550, ATA_100a },
  88. { "SiS640", PCI_DEVICE_ID_SI_640, ATA_66 },
  89. { "SiS630", PCI_DEVICE_ID_SI_630, ATA_66 },
  90. { "SiS620", PCI_DEVICE_ID_SI_620, ATA_66 },
  91. { "SiS540", PCI_DEVICE_ID_SI_540, ATA_66 },
  92. { "SiS530", PCI_DEVICE_ID_SI_530, ATA_66 },
  93. { "SiS5600", PCI_DEVICE_ID_SI_5600, ATA_33 },
  94. { "SiS5598", PCI_DEVICE_ID_SI_5598, ATA_33 },
  95. { "SiS5597", PCI_DEVICE_ID_SI_5597, ATA_33 },
  96. { "SiS5591/2", PCI_DEVICE_ID_SI_5591, ATA_33 },
  97. { "SiS5582", PCI_DEVICE_ID_SI_5582, ATA_33 },
  98. { "SiS5581", PCI_DEVICE_ID_SI_5581, ATA_33 },
  99. { "SiS5596", PCI_DEVICE_ID_SI_5596, ATA_16 },
  100. { "SiS5571", PCI_DEVICE_ID_SI_5571, ATA_16 },
  101. { "SiS5517", PCI_DEVICE_ID_SI_5517, ATA_16 },
  102. { "SiS551x", PCI_DEVICE_ID_SI_5511, ATA_16 },
  103. };
  104. /* Cycle time bits and values vary across chip dma capabilities
  105. These three arrays hold the register layout and the values to set.
  106. Indexed by chipset_family and (dma_mode - XFER_UDMA_0) */
  107. /* {0, ATA_16, ATA_33, ATA_66, ATA_100a, ATA_100, ATA_133} */
  108. static u8 cycle_time_offset[] = {0,0,5,4,4,0,0};
  109. static u8 cycle_time_range[] = {0,0,2,3,3,4,4};
  110. static u8 cycle_time_value[][XFER_UDMA_6 - XFER_UDMA_0 + 1] = {
  111. {0,0,0,0,0,0,0}, /* no udma */
  112. {0,0,0,0,0,0,0}, /* no udma */
  113. {3,2,1,0,0,0,0}, /* ATA_33 */
  114. {7,5,3,2,1,0,0}, /* ATA_66 */
  115. {7,5,3,2,1,0,0}, /* ATA_100a (730 specific), differences are on cycle_time range and offset */
  116. {11,7,5,4,2,1,0}, /* ATA_100 */
  117. {15,10,7,5,3,2,1}, /* ATA_133a (earliest 691 southbridges) */
  118. {15,10,7,5,3,2,1}, /* ATA_133 */
  119. };
  120. /* CRC Valid Setup Time vary across IDE clock setting 33/66/100/133
  121. See SiS962 data sheet for more detail */
  122. static u8 cvs_time_value[][XFER_UDMA_6 - XFER_UDMA_0 + 1] = {
  123. {0,0,0,0,0,0,0}, /* no udma */
  124. {0,0,0,0,0,0,0}, /* no udma */
  125. {2,1,1,0,0,0,0},
  126. {4,3,2,1,0,0,0},
  127. {4,3,2,1,0,0,0},
  128. {6,4,3,1,1,1,0},
  129. {9,6,4,2,2,2,2},
  130. {9,6,4,2,2,2,2},
  131. };
  132. /* Initialize time, Active time, Recovery time vary across
  133. IDE clock settings. These 3 arrays hold the register value
  134. for PIO0/1/2/3/4 and DMA0/1/2 mode in order */
  135. static u8 ini_time_value[][8] = {
  136. {0,0,0,0,0,0,0,0},
  137. {0,0,0,0,0,0,0,0},
  138. {2,1,0,0,0,1,0,0},
  139. {4,3,1,1,1,3,1,1},
  140. {4,3,1,1,1,3,1,1},
  141. {6,4,2,2,2,4,2,2},
  142. {9,6,3,3,3,6,3,3},
  143. {9,6,3,3,3,6,3,3},
  144. };
  145. static u8 act_time_value[][8] = {
  146. {0,0,0,0,0,0,0,0},
  147. {0,0,0,0,0,0,0,0},
  148. {9,9,9,2,2,7,2,2},
  149. {19,19,19,5,4,14,5,4},
  150. {19,19,19,5,4,14,5,4},
  151. {28,28,28,7,6,21,7,6},
  152. {38,38,38,10,9,28,10,9},
  153. {38,38,38,10,9,28,10,9},
  154. };
  155. static u8 rco_time_value[][8] = {
  156. {0,0,0,0,0,0,0,0},
  157. {0,0,0,0,0,0,0,0},
  158. {9,2,0,2,0,7,1,1},
  159. {19,5,1,5,2,16,3,2},
  160. {19,5,1,5,2,16,3,2},
  161. {30,9,3,9,4,25,6,4},
  162. {40,12,4,12,5,34,12,5},
  163. {40,12,4,12,5,34,12,5},
  164. };
  165. /*
  166. * Printing configuration
  167. */
  168. /* Used for chipset type printing at boot time */
  169. static char* chipset_capability[] = {
  170. "ATA", "ATA 16",
  171. "ATA 33", "ATA 66",
  172. "ATA 100 (1st gen)", "ATA 100 (2nd gen)",
  173. "ATA 133 (1st gen)", "ATA 133 (2nd gen)"
  174. };
  175. #if defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS)
  176. #include <linux/stat.h>
  177. #include <linux/proc_fs.h>
  178. static u8 sis_proc = 0;
  179. static struct pci_dev *bmide_dev;
  180. static char* cable_type[] = {
  181. "80 pins",
  182. "40 pins"
  183. };
  184. static char* recovery_time[] ={
  185. "12 PCICLK", "1 PCICLK",
  186. "2 PCICLK", "3 PCICLK",
  187. "4 PCICLK", "5 PCICLCK",
  188. "6 PCICLK", "7 PCICLCK",
  189. "8 PCICLK", "9 PCICLCK",
  190. "10 PCICLK", "11 PCICLK",
  191. "13 PCICLK", "14 PCICLK",
  192. "15 PCICLK", "15 PCICLK"
  193. };
  194. static char* active_time[] = {
  195. "8 PCICLK", "1 PCICLCK",
  196. "2 PCICLK", "3 PCICLK",
  197. "4 PCICLK", "5 PCICLK",
  198. "6 PCICLK", "12 PCICLK"
  199. };
  200. static char* cycle_time[] = {
  201. "Reserved", "2 CLK",
  202. "3 CLK", "4 CLK",
  203. "5 CLK", "6 CLK",
  204. "7 CLK", "8 CLK",
  205. "9 CLK", "10 CLK",
  206. "11 CLK", "12 CLK",
  207. "13 CLK", "14 CLK",
  208. "15 CLK", "16 CLK"
  209. };
  210. /* Generic add master or slave info function */
  211. static char* get_drives_info (char *buffer, u8 pos)
  212. {
  213. u8 reg00, reg01, reg10, reg11; /* timing registers */
  214. u32 regdw0, regdw1;
  215. char* p = buffer;
  216. /* Postwrite/Prefetch */
  217. if (chipset_family < ATA_133) {
  218. pci_read_config_byte(bmide_dev, 0x4b, &reg00);
  219. p += sprintf(p, "Drive %d: Postwrite %s \t \t Postwrite %s\n",
  220. pos, (reg00 & (0x10 << pos)) ? "Enabled" : "Disabled",
  221. (reg00 & (0x40 << pos)) ? "Enabled" : "Disabled");
  222. p += sprintf(p, " Prefetch %s \t \t Prefetch %s\n",
  223. (reg00 & (0x01 << pos)) ? "Enabled" : "Disabled",
  224. (reg00 & (0x04 << pos)) ? "Enabled" : "Disabled");
  225. pci_read_config_byte(bmide_dev, 0x40+2*pos, &reg00);
  226. pci_read_config_byte(bmide_dev, 0x41+2*pos, &reg01);
  227. pci_read_config_byte(bmide_dev, 0x44+2*pos, &reg10);
  228. pci_read_config_byte(bmide_dev, 0x45+2*pos, &reg11);
  229. } else {
  230. u32 reg54h;
  231. u8 drive_pci = 0x40;
  232. pci_read_config_dword(bmide_dev, 0x54, &reg54h);
  233. if (reg54h & 0x40000000) {
  234. // Configuration space remapped to 0x70
  235. drive_pci = 0x70;
  236. }
  237. pci_read_config_dword(bmide_dev, (unsigned long)drive_pci+4*pos, &regdw0);
  238. pci_read_config_dword(bmide_dev, (unsigned long)drive_pci+4*pos+8, &regdw1);
  239. p += sprintf(p, "Drive %d:\n", pos);
  240. }
  241. /* UDMA */
  242. if (chipset_family >= ATA_133) {
  243. p += sprintf(p, " UDMA %s \t \t \t UDMA %s\n",
  244. (regdw0 & 0x04) ? "Enabled" : "Disabled",
  245. (regdw1 & 0x04) ? "Enabled" : "Disabled");
  246. p += sprintf(p, " UDMA Cycle Time %s \t UDMA Cycle Time %s\n",
  247. cycle_time[(regdw0 & 0xF0) >> 4],
  248. cycle_time[(regdw1 & 0xF0) >> 4]);
  249. } else if (chipset_family >= ATA_33) {
  250. p += sprintf(p, " UDMA %s \t \t \t UDMA %s\n",
  251. (reg01 & 0x80) ? "Enabled" : "Disabled",
  252. (reg11 & 0x80) ? "Enabled" : "Disabled");
  253. p += sprintf(p, " UDMA Cycle Time ");
  254. switch(chipset_family) {
  255. case ATA_33: p += sprintf(p, cycle_time[(reg01 & 0x60) >> 5]); break;
  256. case ATA_66:
  257. case ATA_100a: p += sprintf(p, cycle_time[(reg01 & 0x70) >> 4]); break;
  258. case ATA_100:
  259. case ATA_133a: p += sprintf(p, cycle_time[reg01 & 0x0F]); break;
  260. default: p += sprintf(p, "?"); break;
  261. }
  262. p += sprintf(p, " \t UDMA Cycle Time ");
  263. switch(chipset_family) {
  264. case ATA_33: p += sprintf(p, cycle_time[(reg11 & 0x60) >> 5]); break;
  265. case ATA_66:
  266. case ATA_100a: p += sprintf(p, cycle_time[(reg11 & 0x70) >> 4]); break;
  267. case ATA_100:
  268. case ATA_133a: p += sprintf(p, cycle_time[reg11 & 0x0F]); break;
  269. default: p += sprintf(p, "?"); break;
  270. }
  271. p += sprintf(p, "\n");
  272. }
  273. if (chipset_family < ATA_133) { /* else case TODO */
  274. /* Data Active */
  275. p += sprintf(p, " Data Active Time ");
  276. switch(chipset_family) {
  277. case ATA_16: /* confirmed */
  278. case ATA_33:
  279. case ATA_66:
  280. case ATA_100a: p += sprintf(p, active_time[reg01 & 0x07]); break;
  281. case ATA_100:
  282. case ATA_133a: p += sprintf(p, active_time[(reg00 & 0x70) >> 4]); break;
  283. default: p += sprintf(p, "?"); break;
  284. }
  285. p += sprintf(p, " \t Data Active Time ");
  286. switch(chipset_family) {
  287. case ATA_16:
  288. case ATA_33:
  289. case ATA_66:
  290. case ATA_100a: p += sprintf(p, active_time[reg11 & 0x07]); break;
  291. case ATA_100:
  292. case ATA_133a: p += sprintf(p, active_time[(reg10 & 0x70) >> 4]); break;
  293. default: p += sprintf(p, "?"); break;
  294. }
  295. p += sprintf(p, "\n");
  296. /* Data Recovery */
  297. /* warning: may need (reg&0x07) for pre ATA66 chips */
  298. p += sprintf(p, " Data Recovery Time %s \t Data Recovery Time %s\n",
  299. recovery_time[reg00 & 0x0f], recovery_time[reg10 & 0x0f]);
  300. }
  301. return p;
  302. }
  303. static char* get_masters_info(char* buffer)
  304. {
  305. return get_drives_info(buffer, 0);
  306. }
  307. static char* get_slaves_info(char* buffer)
  308. {
  309. return get_drives_info(buffer, 1);
  310. }
  311. /* Main get_info, called on /proc/ide/sis reads */
  312. static int sis_get_info (char *buffer, char **addr, off_t offset, int count)
  313. {
  314. char *p = buffer;
  315. int len;
  316. u8 reg;
  317. u16 reg2, reg3;
  318. p += sprintf(p, "\nSiS 5513 ");
  319. switch(chipset_family) {
  320. case ATA_16: p += sprintf(p, "DMA 16"); break;
  321. case ATA_33: p += sprintf(p, "Ultra 33"); break;
  322. case ATA_66: p += sprintf(p, "Ultra 66"); break;
  323. case ATA_100a:
  324. case ATA_100: p += sprintf(p, "Ultra 100"); break;
  325. case ATA_133a:
  326. case ATA_133: p += sprintf(p, "Ultra 133"); break;
  327. default: p+= sprintf(p, "Unknown???"); break;
  328. }
  329. p += sprintf(p, " chipset\n");
  330. p += sprintf(p, "--------------- Primary Channel "
  331. "---------------- Secondary Channel "
  332. "-------------\n");
  333. /* Status */
  334. pci_read_config_byte(bmide_dev, 0x4a, &reg);
  335. if (chipset_family == ATA_133) {
  336. pci_read_config_word(bmide_dev, 0x50, &reg2);
  337. pci_read_config_word(bmide_dev, 0x52, &reg3);
  338. }
  339. p += sprintf(p, "Channel Status: ");
  340. if (chipset_family < ATA_66) {
  341. p += sprintf(p, "%s \t \t \t \t %s\n",
  342. (reg & 0x04) ? "On" : "Off",
  343. (reg & 0x02) ? "On" : "Off");
  344. } else if (chipset_family < ATA_133) {
  345. p += sprintf(p, "%s \t \t \t \t %s \n",
  346. (reg & 0x02) ? "On" : "Off",
  347. (reg & 0x04) ? "On" : "Off");
  348. } else { /* ATA_133 */
  349. p += sprintf(p, "%s \t \t \t \t %s \n",
  350. (reg2 & 0x02) ? "On" : "Off",
  351. (reg3 & 0x02) ? "On" : "Off");
  352. }
  353. /* Operation Mode */
  354. pci_read_config_byte(bmide_dev, 0x09, &reg);
  355. p += sprintf(p, "Operation Mode: %s \t \t \t %s \n",
  356. (reg & 0x01) ? "Native" : "Compatible",
  357. (reg & 0x04) ? "Native" : "Compatible");
  358. /* 80-pin cable ? */
  359. if (chipset_family >= ATA_133) {
  360. p += sprintf(p, "Cable Type: %s \t \t \t %s\n",
  361. (reg2 & 0x01) ? cable_type[1] : cable_type[0],
  362. (reg3 & 0x01) ? cable_type[1] : cable_type[0]);
  363. } else if (chipset_family > ATA_33) {
  364. pci_read_config_byte(bmide_dev, 0x48, &reg);
  365. p += sprintf(p, "Cable Type: %s \t \t \t %s\n",
  366. (reg & 0x10) ? cable_type[1] : cable_type[0],
  367. (reg & 0x20) ? cable_type[1] : cable_type[0]);
  368. }
  369. /* Prefetch Count */
  370. if (chipset_family < ATA_133) {
  371. pci_read_config_word(bmide_dev, 0x4c, &reg2);
  372. pci_read_config_word(bmide_dev, 0x4e, &reg3);
  373. p += sprintf(p, "Prefetch Count: %d \t \t \t \t %d\n",
  374. reg2, reg3);
  375. }
  376. p = get_masters_info(p);
  377. p = get_slaves_info(p);
  378. len = (p - buffer) - offset;
  379. *addr = buffer + offset;
  380. return len > count ? count : len;
  381. }
  382. #endif /* defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS) */
  383. static u8 sis5513_ratemask (ide_drive_t *drive)
  384. {
  385. u8 rates[] = { 0, 0, 1, 2, 3, 3, 4, 4 };
  386. u8 mode = rates[chipset_family];
  387. if (!eighty_ninty_three(drive))
  388. mode = min(mode, (u8)1);
  389. return mode;
  390. }
  391. /*
  392. * Configuration functions
  393. */
  394. /* Enables per-drive prefetch and postwrite */
  395. static void config_drive_art_rwp (ide_drive_t *drive)
  396. {
  397. ide_hwif_t *hwif = HWIF(drive);
  398. struct pci_dev *dev = hwif->pci_dev;
  399. u8 reg4bh = 0;
  400. u8 rw_prefetch = (0x11 << drive->dn);
  401. if (drive->media != ide_disk)
  402. return;
  403. pci_read_config_byte(dev, 0x4b, &reg4bh);
  404. if ((reg4bh & rw_prefetch) != rw_prefetch)
  405. pci_write_config_byte(dev, 0x4b, reg4bh|rw_prefetch);
  406. }
  407. /* Set per-drive active and recovery time */
  408. static void config_art_rwp_pio (ide_drive_t *drive, u8 pio)
  409. {
  410. ide_hwif_t *hwif = HWIF(drive);
  411. struct pci_dev *dev = hwif->pci_dev;
  412. u8 timing, drive_pci, test1, test2;
  413. u16 eide_pio_timing[6] = {600, 390, 240, 180, 120, 90};
  414. u16 xfer_pio = drive->id->eide_pio_modes;
  415. config_drive_art_rwp(drive);
  416. pio = ide_get_best_pio_mode(drive, 255, pio, NULL);
  417. if (xfer_pio> 4)
  418. xfer_pio = 0;
  419. if (drive->id->eide_pio_iordy > 0) {
  420. for (xfer_pio = 5;
  421. (xfer_pio > 0) &&
  422. (drive->id->eide_pio_iordy > eide_pio_timing[xfer_pio]);
  423. xfer_pio--);
  424. } else {
  425. xfer_pio = (drive->id->eide_pio_modes & 4) ? 0x05 :
  426. (drive->id->eide_pio_modes & 2) ? 0x04 :
  427. (drive->id->eide_pio_modes & 1) ? 0x03 : xfer_pio;
  428. }
  429. timing = (xfer_pio >= pio) ? xfer_pio : pio;
  430. /* In pre ATA_133 case, drives sit at 0x40 + 4*drive->dn */
  431. drive_pci = 0x40;
  432. /* In SiS962 case drives sit at (0x40 or 0x70) + 8*drive->dn) */
  433. if (chipset_family >= ATA_133) {
  434. u32 reg54h;
  435. pci_read_config_dword(dev, 0x54, &reg54h);
  436. if (reg54h & 0x40000000) drive_pci = 0x70;
  437. drive_pci += ((drive->dn)*0x4);
  438. } else {
  439. drive_pci += ((drive->dn)*0x2);
  440. }
  441. /* register layout changed with newer ATA100 chips */
  442. if (chipset_family < ATA_100) {
  443. pci_read_config_byte(dev, drive_pci, &test1);
  444. pci_read_config_byte(dev, drive_pci+1, &test2);
  445. /* Clear active and recovery timings */
  446. test1 &= ~0x0F;
  447. test2 &= ~0x07;
  448. switch(timing) {
  449. case 4: test1 |= 0x01; test2 |= 0x03; break;
  450. case 3: test1 |= 0x03; test2 |= 0x03; break;
  451. case 2: test1 |= 0x04; test2 |= 0x04; break;
  452. case 1: test1 |= 0x07; test2 |= 0x06; break;
  453. default: break;
  454. }
  455. pci_write_config_byte(dev, drive_pci, test1);
  456. pci_write_config_byte(dev, drive_pci+1, test2);
  457. } else if (chipset_family < ATA_133) {
  458. switch(timing) { /* active recovery
  459. v v */
  460. case 4: test1 = 0x30|0x01; break;
  461. case 3: test1 = 0x30|0x03; break;
  462. case 2: test1 = 0x40|0x04; break;
  463. case 1: test1 = 0x60|0x07; break;
  464. case 0: test1 = 0x00; break;
  465. default: break;
  466. }
  467. pci_write_config_byte(dev, drive_pci, test1);
  468. } else { /* ATA_133 */
  469. u32 test3;
  470. pci_read_config_dword(dev, drive_pci, &test3);
  471. test3 &= 0xc0c00fff;
  472. if (test3 & 0x08) {
  473. test3 |= (unsigned long)ini_time_value[ATA_133][timing] << 12;
  474. test3 |= (unsigned long)act_time_value[ATA_133][timing] << 16;
  475. test3 |= (unsigned long)rco_time_value[ATA_133][timing] << 24;
  476. } else {
  477. test3 |= (unsigned long)ini_time_value[ATA_100][timing] << 12;
  478. test3 |= (unsigned long)act_time_value[ATA_100][timing] << 16;
  479. test3 |= (unsigned long)rco_time_value[ATA_100][timing] << 24;
  480. }
  481. pci_write_config_dword(dev, drive_pci, test3);
  482. }
  483. }
  484. static int config_chipset_for_pio (ide_drive_t *drive, u8 pio)
  485. {
  486. if (pio == 255)
  487. pio = ide_find_best_mode(drive, XFER_PIO | XFER_EPIO) - XFER_PIO_0;
  488. config_art_rwp_pio(drive, pio);
  489. return ide_config_drive_speed(drive, XFER_PIO_0 + min_t(u8, pio, 4));
  490. }
  491. static int sis5513_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  492. {
  493. ide_hwif_t *hwif = HWIF(drive);
  494. struct pci_dev *dev = hwif->pci_dev;
  495. u8 drive_pci, reg, speed;
  496. u32 regdw;
  497. speed = ide_rate_filter(sis5513_ratemask(drive), xferspeed);
  498. /* See config_art_rwp_pio for drive pci config registers */
  499. drive_pci = 0x40;
  500. if (chipset_family >= ATA_133) {
  501. u32 reg54h;
  502. pci_read_config_dword(dev, 0x54, &reg54h);
  503. if (reg54h & 0x40000000) drive_pci = 0x70;
  504. drive_pci += ((drive->dn)*0x4);
  505. pci_read_config_dword(dev, (unsigned long)drive_pci, &regdw);
  506. /* Disable UDMA bit for non UDMA modes on UDMA chips */
  507. if (speed < XFER_UDMA_0) {
  508. regdw &= 0xfffffffb;
  509. pci_write_config_dword(dev, (unsigned long)drive_pci, regdw);
  510. }
  511. } else {
  512. drive_pci += ((drive->dn)*0x2);
  513. pci_read_config_byte(dev, drive_pci+1, &reg);
  514. /* Disable UDMA bit for non UDMA modes on UDMA chips */
  515. if ((speed < XFER_UDMA_0) && (chipset_family > ATA_16)) {
  516. reg &= 0x7F;
  517. pci_write_config_byte(dev, drive_pci+1, reg);
  518. }
  519. }
  520. /* Config chip for mode */
  521. switch(speed) {
  522. case XFER_UDMA_6:
  523. case XFER_UDMA_5:
  524. case XFER_UDMA_4:
  525. case XFER_UDMA_3:
  526. case XFER_UDMA_2:
  527. case XFER_UDMA_1:
  528. case XFER_UDMA_0:
  529. if (chipset_family >= ATA_133) {
  530. regdw |= 0x04;
  531. regdw &= 0xfffff00f;
  532. /* check if ATA133 enable */
  533. if (regdw & 0x08) {
  534. regdw |= (unsigned long)cycle_time_value[ATA_133][speed-XFER_UDMA_0] << 4;
  535. regdw |= (unsigned long)cvs_time_value[ATA_133][speed-XFER_UDMA_0] << 8;
  536. } else {
  537. /* if ATA133 disable, we should not set speed above UDMA5 */
  538. if (speed > XFER_UDMA_5)
  539. speed = XFER_UDMA_5;
  540. regdw |= (unsigned long)cycle_time_value[ATA_100][speed-XFER_UDMA_0] << 4;
  541. regdw |= (unsigned long)cvs_time_value[ATA_100][speed-XFER_UDMA_0] << 8;
  542. }
  543. pci_write_config_dword(dev, (unsigned long)drive_pci, regdw);
  544. } else {
  545. /* Force the UDMA bit on if we want to use UDMA */
  546. reg |= 0x80;
  547. /* clean reg cycle time bits */
  548. reg &= ~((0xFF >> (8 - cycle_time_range[chipset_family]))
  549. << cycle_time_offset[chipset_family]);
  550. /* set reg cycle time bits */
  551. reg |= cycle_time_value[chipset_family][speed-XFER_UDMA_0]
  552. << cycle_time_offset[chipset_family];
  553. pci_write_config_byte(dev, drive_pci+1, reg);
  554. }
  555. break;
  556. case XFER_MW_DMA_2:
  557. case XFER_MW_DMA_1:
  558. case XFER_MW_DMA_0:
  559. case XFER_SW_DMA_2:
  560. case XFER_SW_DMA_1:
  561. case XFER_SW_DMA_0:
  562. break;
  563. case XFER_PIO_4: return((int) config_chipset_for_pio(drive, 4));
  564. case XFER_PIO_3: return((int) config_chipset_for_pio(drive, 3));
  565. case XFER_PIO_2: return((int) config_chipset_for_pio(drive, 2));
  566. case XFER_PIO_1: return((int) config_chipset_for_pio(drive, 1));
  567. case XFER_PIO_0:
  568. default: return((int) config_chipset_for_pio(drive, 0));
  569. }
  570. return ((int) ide_config_drive_speed(drive, speed));
  571. }
  572. static void sis5513_tune_drive (ide_drive_t *drive, u8 pio)
  573. {
  574. (void) config_chipset_for_pio(drive, pio);
  575. }
  576. /*
  577. * ((id->hw_config & 0x4000|0x2000) && (HWIF(drive)->udma_four))
  578. */
  579. static int config_chipset_for_dma (ide_drive_t *drive)
  580. {
  581. u8 speed = ide_dma_speed(drive, sis5513_ratemask(drive));
  582. #ifdef DEBUG
  583. printk("SIS5513: config_chipset_for_dma, drive %d, ultra %x\n",
  584. drive->dn, drive->id->dma_ultra);
  585. #endif
  586. if (!(speed))
  587. return 0;
  588. sis5513_tune_chipset(drive, speed);
  589. return ide_dma_enable(drive);
  590. }
  591. static int sis5513_config_drive_xfer_rate (ide_drive_t *drive)
  592. {
  593. ide_hwif_t *hwif = HWIF(drive);
  594. struct hd_driveid *id = drive->id;
  595. drive->init_speed = 0;
  596. if (id && (id->capability & 1) && drive->autodma) {
  597. if (ide_use_dma(drive)) {
  598. if (config_chipset_for_dma(drive))
  599. return hwif->ide_dma_on(drive);
  600. }
  601. goto fast_ata_pio;
  602. } else if ((id->capability & 8) || (id->field_valid & 2)) {
  603. fast_ata_pio:
  604. sis5513_tune_drive(drive, 5);
  605. return hwif->ide_dma_off_quietly(drive);
  606. }
  607. /* IORDY not supported */
  608. return 0;
  609. }
  610. /* initiates/aborts (U)DMA read/write operations on a drive. */
  611. static int sis5513_config_xfer_rate (ide_drive_t *drive)
  612. {
  613. config_drive_art_rwp(drive);
  614. config_art_rwp_pio(drive, 5);
  615. return sis5513_config_drive_xfer_rate(drive);
  616. }
  617. /*
  618. Future simpler config_xfer_rate :
  619. When ide_find_best_mode is made bad-drive aware
  620. - remove config_drive_xfer_rate and config_chipset_for_dma,
  621. - replace config_xfer_rate with the following
  622. static int sis5513_config_xfer_rate (ide_drive_t *drive)
  623. {
  624. u16 w80 = HWIF(drive)->udma_four;
  625. u16 speed;
  626. config_drive_art_rwp(drive);
  627. config_art_rwp_pio(drive, 5);
  628. speed = ide_find_best_mode(drive,
  629. XFER_PIO | XFER_EPIO | XFER_SWDMA | XFER_MWDMA |
  630. (chipset_family >= ATA_33 ? XFER_UDMA : 0) |
  631. (w80 && chipset_family >= ATA_66 ? XFER_UDMA_66 : 0) |
  632. (w80 && chipset_family >= ATA_100a ? XFER_UDMA_100 : 0) |
  633. (w80 && chipset_family >= ATA_133a ? XFER_UDMA_133 : 0));
  634. sis5513_tune_chipset(drive, speed);
  635. if (drive->autodma && (speed & XFER_MODE) != XFER_PIO)
  636. return HWIF(drive)->ide_dma_on(drive);
  637. return HWIF(drive)->ide_dma_off_quietly(drive);
  638. }
  639. */
  640. /* Chip detection and general config */
  641. static unsigned int __devinit init_chipset_sis5513 (struct pci_dev *dev, const char *name)
  642. {
  643. struct pci_dev *host;
  644. int i = 0;
  645. chipset_family = 0;
  646. for (i = 0; i < ARRAY_SIZE(SiSHostChipInfo) && !chipset_family; i++) {
  647. host = pci_find_device(PCI_VENDOR_ID_SI, SiSHostChipInfo[i].host_id, NULL);
  648. if (!host)
  649. continue;
  650. chipset_family = SiSHostChipInfo[i].chipset_family;
  651. /* Special case for SiS630 : 630S/ET is ATA_100a */
  652. if (SiSHostChipInfo[i].host_id == PCI_DEVICE_ID_SI_630) {
  653. u8 hostrev;
  654. pci_read_config_byte(host, PCI_REVISION_ID, &hostrev);
  655. if (hostrev >= 0x30)
  656. chipset_family = ATA_100a;
  657. }
  658. printk(KERN_INFO "SIS5513: %s %s controller\n",
  659. SiSHostChipInfo[i].name, chipset_capability[chipset_family]);
  660. }
  661. if (!chipset_family) { /* Belongs to pci-quirks */
  662. u32 idemisc;
  663. u16 trueid;
  664. /* Disable ID masking and register remapping */
  665. pci_read_config_dword(dev, 0x54, &idemisc);
  666. pci_write_config_dword(dev, 0x54, (idemisc & 0x7fffffff));
  667. pci_read_config_word(dev, PCI_DEVICE_ID, &trueid);
  668. pci_write_config_dword(dev, 0x54, idemisc);
  669. if (trueid == 0x5518) {
  670. printk(KERN_INFO "SIS5513: SiS 962/963 MuTIOL IDE UDMA133 controller\n");
  671. chipset_family = ATA_133;
  672. /* Check for 5513 compability mapping
  673. * We must use this, else the port enabled code will fail,
  674. * as it expects the enablebits at 0x4a.
  675. */
  676. if ((idemisc & 0x40000000) == 0) {
  677. pci_write_config_dword(dev, 0x54, idemisc | 0x40000000);
  678. printk(KERN_INFO "SIS5513: Switching to 5513 register mapping\n");
  679. }
  680. }
  681. }
  682. if (!chipset_family) { /* Belongs to pci-quirks */
  683. struct pci_dev *lpc_bridge;
  684. u16 trueid;
  685. u8 prefctl;
  686. u8 idecfg;
  687. u8 sbrev;
  688. pci_read_config_byte(dev, 0x4a, &idecfg);
  689. pci_write_config_byte(dev, 0x4a, idecfg | 0x10);
  690. pci_read_config_word(dev, PCI_DEVICE_ID, &trueid);
  691. pci_write_config_byte(dev, 0x4a, idecfg);
  692. if (trueid == 0x5517) { /* SiS 961/961B */
  693. lpc_bridge = pci_find_slot(0x00, 0x10); /* Bus 0, Dev 2, Fn 0 */
  694. pci_read_config_byte(lpc_bridge, PCI_REVISION_ID, &sbrev);
  695. pci_read_config_byte(dev, 0x49, &prefctl);
  696. if (sbrev == 0x10 && (prefctl & 0x80)) {
  697. printk(KERN_INFO "SIS5513: SiS 961B MuTIOL IDE UDMA133 controller\n");
  698. chipset_family = ATA_133a;
  699. } else {
  700. printk(KERN_INFO "SIS5513: SiS 961 MuTIOL IDE UDMA100 controller\n");
  701. chipset_family = ATA_100;
  702. }
  703. }
  704. }
  705. if (!chipset_family)
  706. return -1;
  707. /* Make general config ops here
  708. 1/ tell IDE channels to operate in Compatibility mode only
  709. 2/ tell old chips to allow per drive IDE timings */
  710. {
  711. u8 reg;
  712. u16 regw;
  713. switch(chipset_family) {
  714. case ATA_133:
  715. /* SiS962 operation mode */
  716. pci_read_config_word(dev, 0x50, &regw);
  717. if (regw & 0x08)
  718. pci_write_config_word(dev, 0x50, regw&0xfff7);
  719. pci_read_config_word(dev, 0x52, &regw);
  720. if (regw & 0x08)
  721. pci_write_config_word(dev, 0x52, regw&0xfff7);
  722. break;
  723. case ATA_133a:
  724. case ATA_100:
  725. /* Fixup latency */
  726. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x80);
  727. /* Set compatibility bit */
  728. pci_read_config_byte(dev, 0x49, &reg);
  729. if (!(reg & 0x01)) {
  730. pci_write_config_byte(dev, 0x49, reg|0x01);
  731. }
  732. break;
  733. case ATA_100a:
  734. case ATA_66:
  735. /* Fixup latency */
  736. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x10);
  737. /* On ATA_66 chips the bit was elsewhere */
  738. pci_read_config_byte(dev, 0x52, &reg);
  739. if (!(reg & 0x04)) {
  740. pci_write_config_byte(dev, 0x52, reg|0x04);
  741. }
  742. break;
  743. case ATA_33:
  744. /* On ATA_33 we didn't have a single bit to set */
  745. pci_read_config_byte(dev, 0x09, &reg);
  746. if ((reg & 0x0f) != 0x00) {
  747. pci_write_config_byte(dev, 0x09, reg&0xf0);
  748. }
  749. case ATA_16:
  750. /* force per drive recovery and active timings
  751. needed on ATA_33 and below chips */
  752. pci_read_config_byte(dev, 0x52, &reg);
  753. if (!(reg & 0x08)) {
  754. pci_write_config_byte(dev, 0x52, reg|0x08);
  755. }
  756. break;
  757. }
  758. #if defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS)
  759. if (!sis_proc) {
  760. sis_proc = 1;
  761. bmide_dev = dev;
  762. ide_pci_create_host_proc("sis", sis_get_info);
  763. }
  764. #endif
  765. }
  766. return 0;
  767. }
  768. static unsigned int __devinit ata66_sis5513 (ide_hwif_t *hwif)
  769. {
  770. u8 ata66 = 0;
  771. if (chipset_family >= ATA_133) {
  772. u16 regw = 0;
  773. u16 reg_addr = hwif->channel ? 0x52: 0x50;
  774. pci_read_config_word(hwif->pci_dev, reg_addr, &regw);
  775. ata66 = (regw & 0x8000) ? 0 : 1;
  776. } else if (chipset_family >= ATA_66) {
  777. u8 reg48h = 0;
  778. u8 mask = hwif->channel ? 0x20 : 0x10;
  779. pci_read_config_byte(hwif->pci_dev, 0x48, &reg48h);
  780. ata66 = (reg48h & mask) ? 0 : 1;
  781. }
  782. return ata66;
  783. }
  784. static void __devinit init_hwif_sis5513 (ide_hwif_t *hwif)
  785. {
  786. hwif->autodma = 0;
  787. if (!hwif->irq)
  788. hwif->irq = hwif->channel ? 15 : 14;
  789. hwif->tuneproc = &sis5513_tune_drive;
  790. hwif->speedproc = &sis5513_tune_chipset;
  791. if (!(hwif->dma_base)) {
  792. hwif->drives[0].autotune = 1;
  793. hwif->drives[1].autotune = 1;
  794. return;
  795. }
  796. hwif->atapi_dma = 1;
  797. hwif->ultra_mask = 0x7f;
  798. hwif->mwdma_mask = 0x07;
  799. hwif->swdma_mask = 0x07;
  800. if (!chipset_family)
  801. return;
  802. if (!(hwif->udma_four))
  803. hwif->udma_four = ata66_sis5513(hwif);
  804. if (chipset_family > ATA_16) {
  805. hwif->ide_dma_check = &sis5513_config_xfer_rate;
  806. if (!noautodma)
  807. hwif->autodma = 1;
  808. }
  809. hwif->drives[0].autodma = hwif->autodma;
  810. hwif->drives[1].autodma = hwif->autodma;
  811. return;
  812. }
  813. static ide_pci_device_t sis5513_chipset __devinitdata = {
  814. .name = "SIS5513",
  815. .init_chipset = init_chipset_sis5513,
  816. .init_hwif = init_hwif_sis5513,
  817. .channels = 2,
  818. .autodma = NOAUTODMA,
  819. .enablebits = {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}},
  820. .bootable = ON_BOARD,
  821. };
  822. static int __devinit sis5513_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  823. {
  824. return ide_setup_pci_device(dev, &sis5513_chipset);
  825. }
  826. static struct pci_device_id sis5513_pci_tbl[] = {
  827. { PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  828. { PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5518, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  829. { 0, },
  830. };
  831. MODULE_DEVICE_TABLE(pci, sis5513_pci_tbl);
  832. static struct pci_driver driver = {
  833. .name = "SIS_IDE",
  834. .id_table = sis5513_pci_tbl,
  835. .probe = sis5513_init_one,
  836. };
  837. static int sis5513_ide_init(void)
  838. {
  839. return ide_pci_register_driver(&driver);
  840. }
  841. module_init(sis5513_ide_init);
  842. MODULE_AUTHOR("Lionel Bouton, L C Chang, Andre Hedrick, Vojtech Pavlik");
  843. MODULE_DESCRIPTION("PCI driver module for SIS IDE");
  844. MODULE_LICENSE("GPL");
  845. /*
  846. * TODO:
  847. * - CLEANUP
  848. * - Use drivers/ide/ide-timing.h !
  849. * - More checks in the config registers (force values instead of
  850. * relying on the BIOS setting them correctly).
  851. * - Further optimisations ?
  852. * . for example ATA66+ regs 0x48 & 0x4A
  853. */