sis5513.c 28 KB

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