i2c-piix4.c 24 KB

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