sis5513.c 28 KB

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