i2c-piix4.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
  3. Philip Edelbrock <phil@netroedge.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. */
  13. /*
  14. Supports:
  15. Intel PIIX4, 440MX
  16. Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
  17. ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
  18. AMD Hudson-2, ML, CZ
  19. SMSC Victory66
  20. Note: we assume there can only be one device, with one or more
  21. SMBus interfaces.
  22. The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS).
  23. For devices supporting multiple ports the i2c_adapter should provide
  24. an i2c_algorithm to access them.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/pci.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/stddef.h>
  32. #include <linux/ioport.h>
  33. #include <linux/i2c.h>
  34. #include <linux/slab.h>
  35. #include <linux/dmi.h>
  36. #include <linux/acpi.h>
  37. #include <linux/io.h>
  38. #include <linux/mutex.h>
  39. /* PIIX4 SMBus address offsets */
  40. #define SMBHSTSTS (0 + piix4_smba)
  41. #define SMBHSLVSTS (1 + piix4_smba)
  42. #define SMBHSTCNT (2 + piix4_smba)
  43. #define SMBHSTCMD (3 + piix4_smba)
  44. #define SMBHSTADD (4 + piix4_smba)
  45. #define SMBHSTDAT0 (5 + piix4_smba)
  46. #define SMBHSTDAT1 (6 + piix4_smba)
  47. #define SMBBLKDAT (7 + piix4_smba)
  48. #define SMBSLVCNT (8 + piix4_smba)
  49. #define SMBSHDWCMD (9 + piix4_smba)
  50. #define SMBSLVEVT (0xA + piix4_smba)
  51. #define SMBSLVDAT (0xC + piix4_smba)
  52. /* count for request_region */
  53. #define SMBIOSIZE 9
  54. /* PCI Address Constants */
  55. #define SMBBA 0x090
  56. #define SMBHSTCFG 0x0D2
  57. #define SMBSLVC 0x0D3
  58. #define SMBSHDW1 0x0D4
  59. #define SMBSHDW2 0x0D5
  60. #define SMBREV 0x0D6
  61. /* Other settings */
  62. #define MAX_TIMEOUT 500
  63. #define ENABLE_INT9 0
  64. /* PIIX4 constants */
  65. #define PIIX4_QUICK 0x00
  66. #define PIIX4_BYTE 0x04
  67. #define PIIX4_BYTE_DATA 0x08
  68. #define PIIX4_WORD_DATA 0x0C
  69. #define PIIX4_BLOCK_DATA 0x14
  70. /* Multi-port constants */
  71. #define PIIX4_MAX_ADAPTERS 4
  72. /* SB800 constants */
  73. #define SB800_PIIX4_SMB_IDX 0xcd6
  74. /*
  75. * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
  76. * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
  77. * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
  78. */
  79. #define SB800_PIIX4_PORT_IDX 0x2c
  80. #define SB800_PIIX4_PORT_IDX_ALT 0x2e
  81. #define SB800_PIIX4_PORT_IDX_SEL 0x2f
  82. #define SB800_PIIX4_PORT_IDX_MASK 0x06
  83. #define SB800_PIIX4_PORT_IDX_SHIFT 1
  84. /* On kerncz, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
  85. #define SB800_PIIX4_PORT_IDX_KERNCZ 0x02
  86. #define SB800_PIIX4_PORT_IDX_MASK_KERNCZ 0x18
  87. #define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ 3
  88. /* insmod parameters */
  89. /* If force is set to anything different from 0, we forcibly enable the
  90. PIIX4. DANGEROUS! */
  91. static int force;
  92. module_param (force, int, 0);
  93. MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
  94. /* If force_addr is set to anything different from 0, we forcibly enable
  95. the PIIX4 at the given address. VERY DANGEROUS! */
  96. static int force_addr;
  97. module_param (force_addr, int, 0);
  98. MODULE_PARM_DESC(force_addr,
  99. "Forcibly enable the PIIX4 at the given address. "
  100. "EXTREMELY DANGEROUS!");
  101. static int srvrworks_csb5_delay;
  102. static struct pci_driver piix4_driver;
  103. static const struct dmi_system_id piix4_dmi_blacklist[] = {
  104. {
  105. .ident = "Sapphire AM2RD790",
  106. .matches = {
  107. DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
  108. DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
  109. },
  110. },
  111. {
  112. .ident = "DFI Lanparty UT 790FX",
  113. .matches = {
  114. DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
  115. DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
  116. },
  117. },
  118. { }
  119. };
  120. /* The IBM entry is in a separate table because we only check it
  121. on Intel-based systems */
  122. static const struct dmi_system_id piix4_dmi_ibm[] = {
  123. {
  124. .ident = "IBM",
  125. .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
  126. },
  127. { },
  128. };
  129. /*
  130. * SB800 globals
  131. * piix4_mutex_sb800 protects piix4_port_sel_sb800 and the pair
  132. * of I/O ports at SB800_PIIX4_SMB_IDX.
  133. */
  134. static DEFINE_MUTEX(piix4_mutex_sb800);
  135. static u8 piix4_port_sel_sb800;
  136. static u8 piix4_port_mask_sb800;
  137. static u8 piix4_port_shift_sb800;
  138. static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
  139. " port 0", " port 2", " port 3", " port 4"
  140. };
  141. static const char *piix4_aux_port_name_sb800 = " port 1";
  142. struct i2c_piix4_adapdata {
  143. unsigned short smba;
  144. /* SB800 */
  145. bool sb800_main;
  146. u8 port; /* Port number, shifted */
  147. };
  148. static int piix4_setup(struct pci_dev *PIIX4_dev,
  149. const struct pci_device_id *id)
  150. {
  151. unsigned char temp;
  152. unsigned short piix4_smba;
  153. if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
  154. (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
  155. srvrworks_csb5_delay = 1;
  156. /* On some motherboards, it was reported that accessing the SMBus
  157. caused severe hardware problems */
  158. if (dmi_check_system(piix4_dmi_blacklist)) {
  159. dev_err(&PIIX4_dev->dev,
  160. "Accessing the SMBus on this system is unsafe!\n");
  161. return -EPERM;
  162. }
  163. /* Don't access SMBus on IBM systems which get corrupted eeproms */
  164. if (dmi_check_system(piix4_dmi_ibm) &&
  165. PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
  166. dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
  167. "may corrupt your serial eeprom! Refusing to load "
  168. "module!\n");
  169. return -EPERM;
  170. }
  171. /* Determine the address of the SMBus areas */
  172. if (force_addr) {
  173. piix4_smba = force_addr & 0xfff0;
  174. force = 0;
  175. } else {
  176. pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
  177. piix4_smba &= 0xfff0;
  178. if(piix4_smba == 0) {
  179. dev_err(&PIIX4_dev->dev, "SMBus base address "
  180. "uninitialized - upgrade BIOS or use "
  181. "force_addr=0xaddr\n");
  182. return -ENODEV;
  183. }
  184. }
  185. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  186. return -ENODEV;
  187. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  188. dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
  189. piix4_smba);
  190. return -EBUSY;
  191. }
  192. pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
  193. /* If force_addr is set, we program the new address here. Just to make
  194. sure, we disable the PIIX4 first. */
  195. if (force_addr) {
  196. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
  197. pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
  198. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
  199. dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
  200. "new address %04x!\n", piix4_smba);
  201. } else if ((temp & 1) == 0) {
  202. if (force) {
  203. /* This should never need to be done, but has been
  204. * noted that many Dell machines have the SMBus
  205. * interface on the PIIX4 disabled!? NOTE: This assumes
  206. * I/O space and other allocations WERE done by the
  207. * Bios! Don't complain if your hardware does weird
  208. * things after enabling this. :') Check for Bios
  209. * updates before resorting to this.
  210. */
  211. pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
  212. temp | 1);
  213. dev_notice(&PIIX4_dev->dev,
  214. "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
  215. } else {
  216. dev_err(&PIIX4_dev->dev,
  217. "SMBus Host Controller not enabled!\n");
  218. release_region(piix4_smba, SMBIOSIZE);
  219. return -ENODEV;
  220. }
  221. }
  222. if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
  223. dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
  224. else if ((temp & 0x0E) == 0)
  225. dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
  226. else
  227. dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
  228. "(or code out of date)!\n");
  229. pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
  230. dev_info(&PIIX4_dev->dev,
  231. "SMBus Host Controller at 0x%x, revision %d\n",
  232. piix4_smba, temp);
  233. return piix4_smba;
  234. }
  235. static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
  236. const struct pci_device_id *id, u8 aux)
  237. {
  238. unsigned short piix4_smba;
  239. u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
  240. u8 i2ccfg, i2ccfg_offset = 0x10;
  241. /* SB800 and later SMBus does not support forcing address */
  242. if (force || force_addr) {
  243. dev_err(&PIIX4_dev->dev, "SMBus does not support "
  244. "forcing address!\n");
  245. return -EINVAL;
  246. }
  247. /* Determine the address of the SMBus areas */
  248. if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
  249. PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
  250. PIIX4_dev->revision >= 0x41) ||
  251. (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
  252. PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
  253. PIIX4_dev->revision >= 0x49))
  254. smb_en = 0x00;
  255. else
  256. smb_en = (aux) ? 0x28 : 0x2c;
  257. mutex_lock(&piix4_mutex_sb800);
  258. outb_p(smb_en, SB800_PIIX4_SMB_IDX);
  259. smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
  260. outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
  261. smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
  262. mutex_unlock(&piix4_mutex_sb800);
  263. if (!smb_en) {
  264. smb_en_status = smba_en_lo & 0x10;
  265. piix4_smba = smba_en_hi << 8;
  266. if (aux)
  267. piix4_smba |= 0x20;
  268. } else {
  269. smb_en_status = smba_en_lo & 0x01;
  270. piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
  271. }
  272. if (!smb_en_status) {
  273. dev_err(&PIIX4_dev->dev,
  274. "SMBus Host Controller not enabled!\n");
  275. return -ENODEV;
  276. }
  277. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  278. return -ENODEV;
  279. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  280. dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
  281. piix4_smba);
  282. return -EBUSY;
  283. }
  284. /* Aux SMBus does not support IRQ information */
  285. if (aux) {
  286. dev_info(&PIIX4_dev->dev,
  287. "Auxiliary SMBus Host Controller at 0x%x\n",
  288. piix4_smba);
  289. return piix4_smba;
  290. }
  291. /* Request the SMBus I2C bus config region */
  292. if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
  293. dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
  294. "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
  295. release_region(piix4_smba, SMBIOSIZE);
  296. return -EBUSY;
  297. }
  298. i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
  299. release_region(piix4_smba + i2ccfg_offset, 1);
  300. if (i2ccfg & 1)
  301. dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
  302. else
  303. dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
  304. dev_info(&PIIX4_dev->dev,
  305. "SMBus Host Controller at 0x%x, revision %d\n",
  306. piix4_smba, i2ccfg >> 4);
  307. /* Find which register is used for port selection */
  308. if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) {
  309. switch (PIIX4_dev->device) {
  310. case PCI_DEVICE_ID_AMD_KERNCZ_SMBUS:
  311. piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
  312. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
  313. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
  314. break;
  315. case PCI_DEVICE_ID_AMD_HUDSON2_SMBUS:
  316. default:
  317. piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
  318. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
  319. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
  320. break;
  321. }
  322. } else {
  323. mutex_lock(&piix4_mutex_sb800);
  324. outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
  325. port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
  326. piix4_port_sel_sb800 = (port_sel & 0x01) ?
  327. SB800_PIIX4_PORT_IDX_ALT :
  328. SB800_PIIX4_PORT_IDX;
  329. piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
  330. piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
  331. mutex_unlock(&piix4_mutex_sb800);
  332. }
  333. dev_info(&PIIX4_dev->dev,
  334. "Using register 0x%02x for SMBus port selection\n",
  335. (unsigned int)piix4_port_sel_sb800);
  336. return piix4_smba;
  337. }
  338. static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
  339. const struct pci_device_id *id,
  340. unsigned short base_reg_addr)
  341. {
  342. /* Set up auxiliary SMBus controllers found on some
  343. * AMD chipsets e.g. SP5100 (SB700 derivative) */
  344. unsigned short piix4_smba;
  345. /* Read address of auxiliary SMBus controller */
  346. pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
  347. if ((piix4_smba & 1) == 0) {
  348. dev_dbg(&PIIX4_dev->dev,
  349. "Auxiliary SMBus controller not enabled\n");
  350. return -ENODEV;
  351. }
  352. piix4_smba &= 0xfff0;
  353. if (piix4_smba == 0) {
  354. dev_dbg(&PIIX4_dev->dev,
  355. "Auxiliary SMBus base address uninitialized\n");
  356. return -ENODEV;
  357. }
  358. if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
  359. return -ENODEV;
  360. if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
  361. dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
  362. "already in use!\n", piix4_smba);
  363. return -EBUSY;
  364. }
  365. dev_info(&PIIX4_dev->dev,
  366. "Auxiliary SMBus Host Controller at 0x%x\n",
  367. piix4_smba);
  368. return piix4_smba;
  369. }
  370. static int piix4_transaction(struct i2c_adapter *piix4_adapter)
  371. {
  372. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
  373. unsigned short piix4_smba = adapdata->smba;
  374. int temp;
  375. int result = 0;
  376. int timeout = 0;
  377. dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
  378. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  379. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  380. inb_p(SMBHSTDAT1));
  381. /* Make sure the SMBus host is ready to start transmitting */
  382. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  383. dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
  384. "Resetting...\n", temp);
  385. outb_p(temp, SMBHSTSTS);
  386. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  387. dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
  388. return -EBUSY;
  389. } else {
  390. dev_dbg(&piix4_adapter->dev, "Successful!\n");
  391. }
  392. }
  393. /* start the transaction by setting bit 6 */
  394. outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
  395. /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
  396. if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
  397. msleep(2);
  398. else
  399. msleep(1);
  400. while ((++timeout < MAX_TIMEOUT) &&
  401. ((temp = inb_p(SMBHSTSTS)) & 0x01))
  402. msleep(1);
  403. /* If the SMBus is still busy, we give up */
  404. if (timeout == MAX_TIMEOUT) {
  405. dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
  406. result = -ETIMEDOUT;
  407. }
  408. if (temp & 0x10) {
  409. result = -EIO;
  410. dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
  411. }
  412. if (temp & 0x08) {
  413. result = -EIO;
  414. dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
  415. "locked until next hard reset. (sorry!)\n");
  416. /* Clock stops and slave is stuck in mid-transmission */
  417. }
  418. if (temp & 0x04) {
  419. result = -ENXIO;
  420. dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
  421. }
  422. if (inb_p(SMBHSTSTS) != 0x00)
  423. outb_p(inb(SMBHSTSTS), SMBHSTSTS);
  424. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  425. dev_err(&piix4_adapter->dev, "Failed reset at end of "
  426. "transaction (%02x)\n", temp);
  427. }
  428. dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
  429. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  430. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  431. inb_p(SMBHSTDAT1));
  432. return result;
  433. }
  434. /* Return negative errno on error. */
  435. static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
  436. unsigned short flags, char read_write,
  437. u8 command, int size, union i2c_smbus_data * data)
  438. {
  439. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  440. unsigned short piix4_smba = adapdata->smba;
  441. int i, len;
  442. int status;
  443. switch (size) {
  444. case I2C_SMBUS_QUICK:
  445. outb_p((addr << 1) | read_write,
  446. SMBHSTADD);
  447. size = PIIX4_QUICK;
  448. break;
  449. case I2C_SMBUS_BYTE:
  450. outb_p((addr << 1) | read_write,
  451. SMBHSTADD);
  452. if (read_write == I2C_SMBUS_WRITE)
  453. outb_p(command, SMBHSTCMD);
  454. size = PIIX4_BYTE;
  455. break;
  456. case I2C_SMBUS_BYTE_DATA:
  457. outb_p((addr << 1) | read_write,
  458. SMBHSTADD);
  459. outb_p(command, SMBHSTCMD);
  460. if (read_write == I2C_SMBUS_WRITE)
  461. outb_p(data->byte, SMBHSTDAT0);
  462. size = PIIX4_BYTE_DATA;
  463. break;
  464. case I2C_SMBUS_WORD_DATA:
  465. outb_p((addr << 1) | read_write,
  466. SMBHSTADD);
  467. outb_p(command, SMBHSTCMD);
  468. if (read_write == I2C_SMBUS_WRITE) {
  469. outb_p(data->word & 0xff, SMBHSTDAT0);
  470. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  471. }
  472. size = PIIX4_WORD_DATA;
  473. break;
  474. case I2C_SMBUS_BLOCK_DATA:
  475. outb_p((addr << 1) | read_write,
  476. SMBHSTADD);
  477. outb_p(command, SMBHSTCMD);
  478. if (read_write == I2C_SMBUS_WRITE) {
  479. len = data->block[0];
  480. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  481. return -EINVAL;
  482. outb_p(len, SMBHSTDAT0);
  483. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  484. for (i = 1; i <= len; i++)
  485. outb_p(data->block[i], SMBBLKDAT);
  486. }
  487. size = PIIX4_BLOCK_DATA;
  488. break;
  489. default:
  490. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  491. return -EOPNOTSUPP;
  492. }
  493. outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
  494. status = piix4_transaction(adap);
  495. if (status)
  496. return status;
  497. if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
  498. return 0;
  499. switch (size) {
  500. case PIIX4_BYTE:
  501. case PIIX4_BYTE_DATA:
  502. data->byte = inb_p(SMBHSTDAT0);
  503. break;
  504. case PIIX4_WORD_DATA:
  505. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  506. break;
  507. case PIIX4_BLOCK_DATA:
  508. data->block[0] = inb_p(SMBHSTDAT0);
  509. if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
  510. return -EPROTO;
  511. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  512. for (i = 1; i <= data->block[0]; i++)
  513. data->block[i] = inb_p(SMBBLKDAT);
  514. break;
  515. }
  516. return 0;
  517. }
  518. /*
  519. * Handles access to multiple SMBus ports on the SB800.
  520. * The port is selected by bits 2:1 of the smb_en register (0x2c).
  521. * Returns negative errno on error.
  522. *
  523. * Note: The selected port must be returned to the initial selection to avoid
  524. * problems on certain systems.
  525. */
  526. static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
  527. unsigned short flags, char read_write,
  528. u8 command, int size, union i2c_smbus_data *data)
  529. {
  530. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  531. unsigned short piix4_smba = adapdata->smba;
  532. int retries = MAX_TIMEOUT;
  533. int smbslvcnt;
  534. u8 smba_en_lo;
  535. u8 port;
  536. int retval;
  537. mutex_lock(&piix4_mutex_sb800);
  538. /* Request the SMBUS semaphore, avoid conflicts with the IMC */
  539. smbslvcnt = inb_p(SMBSLVCNT);
  540. do {
  541. outb_p(smbslvcnt | 0x10, SMBSLVCNT);
  542. /* Check the semaphore status */
  543. smbslvcnt = inb_p(SMBSLVCNT);
  544. if (smbslvcnt & 0x10)
  545. break;
  546. usleep_range(1000, 2000);
  547. } while (--retries);
  548. /* SMBus is still owned by the IMC, we give up */
  549. if (!retries) {
  550. mutex_unlock(&piix4_mutex_sb800);
  551. return -EBUSY;
  552. }
  553. outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
  554. smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
  555. port = adapdata->port;
  556. if ((smba_en_lo & piix4_port_mask_sb800) != port)
  557. outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
  558. SB800_PIIX4_SMB_IDX + 1);
  559. retval = piix4_access(adap, addr, flags, read_write,
  560. command, size, data);
  561. outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
  562. /* Release the semaphore */
  563. outb_p(smbslvcnt | 0x20, SMBSLVCNT);
  564. mutex_unlock(&piix4_mutex_sb800);
  565. return retval;
  566. }
  567. static u32 piix4_func(struct i2c_adapter *adapter)
  568. {
  569. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  570. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  571. I2C_FUNC_SMBUS_BLOCK_DATA;
  572. }
  573. static const struct i2c_algorithm smbus_algorithm = {
  574. .smbus_xfer = piix4_access,
  575. .functionality = piix4_func,
  576. };
  577. static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
  578. .smbus_xfer = piix4_access_sb800,
  579. .functionality = piix4_func,
  580. };
  581. static const struct pci_device_id piix4_ids[] = {
  582. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
  583. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
  584. { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
  585. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
  586. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
  587. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
  588. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
  589. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
  590. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
  591. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  592. PCI_DEVICE_ID_SERVERWORKS_OSB4) },
  593. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  594. PCI_DEVICE_ID_SERVERWORKS_CSB5) },
  595. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  596. PCI_DEVICE_ID_SERVERWORKS_CSB6) },
  597. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  598. PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
  599. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
  600. PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
  601. { 0, }
  602. };
  603. MODULE_DEVICE_TABLE (pci, piix4_ids);
  604. static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
  605. static struct i2c_adapter *piix4_aux_adapter;
  606. static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
  607. bool sb800_main, u8 port,
  608. const char *name, struct i2c_adapter **padap)
  609. {
  610. struct i2c_adapter *adap;
  611. struct i2c_piix4_adapdata *adapdata;
  612. int retval;
  613. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  614. if (adap == NULL) {
  615. release_region(smba, SMBIOSIZE);
  616. return -ENOMEM;
  617. }
  618. adap->owner = THIS_MODULE;
  619. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  620. adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
  621. : &smbus_algorithm;
  622. adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
  623. if (adapdata == NULL) {
  624. kfree(adap);
  625. release_region(smba, SMBIOSIZE);
  626. return -ENOMEM;
  627. }
  628. adapdata->smba = smba;
  629. adapdata->sb800_main = sb800_main;
  630. adapdata->port = port << piix4_port_shift_sb800;
  631. /* set up the sysfs linkage to our parent device */
  632. adap->dev.parent = &dev->dev;
  633. snprintf(adap->name, sizeof(adap->name),
  634. "SMBus PIIX4 adapter%s at %04x", name, smba);
  635. i2c_set_adapdata(adap, adapdata);
  636. retval = i2c_add_adapter(adap);
  637. if (retval) {
  638. kfree(adapdata);
  639. kfree(adap);
  640. release_region(smba, SMBIOSIZE);
  641. return retval;
  642. }
  643. *padap = adap;
  644. return 0;
  645. }
  646. static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba)
  647. {
  648. struct i2c_piix4_adapdata *adapdata;
  649. int port;
  650. int retval;
  651. for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
  652. retval = piix4_add_adapter(dev, smba, true, port,
  653. piix4_main_port_names_sb800[port],
  654. &piix4_main_adapters[port]);
  655. if (retval < 0)
  656. goto error;
  657. }
  658. return retval;
  659. error:
  660. dev_err(&dev->dev,
  661. "Error setting up SB800 adapters. Unregistering!\n");
  662. while (--port >= 0) {
  663. adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
  664. if (adapdata->smba) {
  665. i2c_del_adapter(piix4_main_adapters[port]);
  666. kfree(adapdata);
  667. kfree(piix4_main_adapters[port]);
  668. piix4_main_adapters[port] = NULL;
  669. }
  670. }
  671. return retval;
  672. }
  673. static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
  674. {
  675. int retval;
  676. bool is_sb800 = false;
  677. if ((dev->vendor == PCI_VENDOR_ID_ATI &&
  678. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
  679. dev->revision >= 0x40) ||
  680. dev->vendor == PCI_VENDOR_ID_AMD) {
  681. is_sb800 = true;
  682. if (!request_region(SB800_PIIX4_SMB_IDX, 2, "smba_idx")) {
  683. dev_err(&dev->dev,
  684. "SMBus base address index region 0x%x already in use!\n",
  685. SB800_PIIX4_SMB_IDX);
  686. return -EBUSY;
  687. }
  688. /* base address location etc changed in SB800 */
  689. retval = piix4_setup_sb800(dev, id, 0);
  690. if (retval < 0) {
  691. release_region(SB800_PIIX4_SMB_IDX, 2);
  692. return retval;
  693. }
  694. /*
  695. * Try to register multiplexed main SMBus adapter,
  696. * give up if we can't
  697. */
  698. retval = piix4_add_adapters_sb800(dev, retval);
  699. if (retval < 0) {
  700. release_region(SB800_PIIX4_SMB_IDX, 2);
  701. return retval;
  702. }
  703. } else {
  704. retval = piix4_setup(dev, id);
  705. if (retval < 0)
  706. return retval;
  707. /* Try to register main SMBus adapter, give up if we can't */
  708. retval = piix4_add_adapter(dev, retval, false, 0, "",
  709. &piix4_main_adapters[0]);
  710. if (retval < 0)
  711. return retval;
  712. }
  713. /* Check for auxiliary SMBus on some AMD chipsets */
  714. retval = -ENODEV;
  715. if (dev->vendor == PCI_VENDOR_ID_ATI &&
  716. dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
  717. if (dev->revision < 0x40) {
  718. retval = piix4_setup_aux(dev, id, 0x58);
  719. } else {
  720. /* SB800 added aux bus too */
  721. retval = piix4_setup_sb800(dev, id, 1);
  722. }
  723. }
  724. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  725. dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) {
  726. retval = piix4_setup_sb800(dev, id, 1);
  727. }
  728. if (retval > 0) {
  729. /* Try to add the aux adapter if it exists,
  730. * piix4_add_adapter will clean up if this fails */
  731. piix4_add_adapter(dev, retval, false, 0,
  732. is_sb800 ? piix4_aux_port_name_sb800 : "",
  733. &piix4_aux_adapter);
  734. }
  735. return 0;
  736. }
  737. static void piix4_adap_remove(struct i2c_adapter *adap)
  738. {
  739. struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
  740. if (adapdata->smba) {
  741. i2c_del_adapter(adap);
  742. if (adapdata->port == (0 << 1)) {
  743. release_region(adapdata->smba, SMBIOSIZE);
  744. if (adapdata->sb800_main)
  745. release_region(SB800_PIIX4_SMB_IDX, 2);
  746. }
  747. kfree(adapdata);
  748. kfree(adap);
  749. }
  750. }
  751. static void piix4_remove(struct pci_dev *dev)
  752. {
  753. int port = PIIX4_MAX_ADAPTERS;
  754. while (--port >= 0) {
  755. if (piix4_main_adapters[port]) {
  756. piix4_adap_remove(piix4_main_adapters[port]);
  757. piix4_main_adapters[port] = NULL;
  758. }
  759. }
  760. if (piix4_aux_adapter) {
  761. piix4_adap_remove(piix4_aux_adapter);
  762. piix4_aux_adapter = NULL;
  763. }
  764. }
  765. static struct pci_driver piix4_driver = {
  766. .name = "piix4_smbus",
  767. .id_table = piix4_ids,
  768. .probe = piix4_probe,
  769. .remove = piix4_remove,
  770. };
  771. module_pci_driver(piix4_driver);
  772. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  773. "Philip Edelbrock <phil@netroedge.com>");
  774. MODULE_DESCRIPTION("PIIX4 SMBus driver");
  775. MODULE_LICENSE("GPL");