sis5513.c 26 KB

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