scx200_acb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
  3. National Semiconductor SCx200 ACCESS.bus support
  4. Also supports the AMD CS5535 and AMD CS5536
  5. Based on i2c-keywest.c which is:
  6. Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  7. Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include <asm/io.h>
  29. #include <asm/msr.h>
  30. #include <linux/scx200.h>
  31. #define NAME "scx200_acb"
  32. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  33. MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
  34. MODULE_LICENSE("GPL");
  35. #define MAX_DEVICES 4
  36. static int base[MAX_DEVICES] = { 0x820, 0x840 };
  37. module_param_array(base, int, NULL, 0);
  38. MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
  39. /* The hardware supports interrupt driven mode too, but I haven't
  40. implemented that. */
  41. #define POLLED_MODE 1
  42. #define POLL_TIMEOUT (HZ)
  43. enum scx200_acb_state {
  44. state_idle,
  45. state_address,
  46. state_command,
  47. state_repeat_start,
  48. state_quick,
  49. state_read,
  50. state_write,
  51. };
  52. static const char *scx200_acb_state_name[] = {
  53. "idle",
  54. "address",
  55. "command",
  56. "repeat_start",
  57. "quick",
  58. "read",
  59. "write",
  60. };
  61. /* Physical interface */
  62. struct scx200_acb_iface {
  63. struct scx200_acb_iface *next;
  64. struct i2c_adapter adapter;
  65. unsigned base;
  66. struct semaphore sem;
  67. /* State machine data */
  68. enum scx200_acb_state state;
  69. int result;
  70. u8 address_byte;
  71. u8 command;
  72. u8 *ptr;
  73. char needs_reset;
  74. unsigned len;
  75. };
  76. /* Register Definitions */
  77. #define ACBSDA (iface->base + 0)
  78. #define ACBST (iface->base + 1)
  79. #define ACBST_SDAST 0x40 /* SDA Status */
  80. #define ACBST_BER 0x20
  81. #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
  82. #define ACBST_STASTR 0x08 /* Stall After Start */
  83. #define ACBST_MASTER 0x02
  84. #define ACBCST (iface->base + 2)
  85. #define ACBCST_BB 0x02
  86. #define ACBCTL1 (iface->base + 3)
  87. #define ACBCTL1_STASTRE 0x80
  88. #define ACBCTL1_NMINTE 0x40
  89. #define ACBCTL1_ACK 0x10
  90. #define ACBCTL1_STOP 0x02
  91. #define ACBCTL1_START 0x01
  92. #define ACBADDR (iface->base + 4)
  93. #define ACBCTL2 (iface->base + 5)
  94. #define ACBCTL2_ENABLE 0x01
  95. /************************************************************************/
  96. static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
  97. {
  98. const char *errmsg;
  99. dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
  100. scx200_acb_state_name[iface->state], status);
  101. if (status & ACBST_BER) {
  102. errmsg = "bus error";
  103. goto error;
  104. }
  105. if (!(status & ACBST_MASTER)) {
  106. errmsg = "not master";
  107. goto error;
  108. }
  109. if (status & ACBST_NEGACK) {
  110. dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
  111. scx200_acb_state_name[iface->state]);
  112. iface->state = state_idle;
  113. iface->result = -ENXIO;
  114. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  115. outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
  116. return;
  117. }
  118. switch (iface->state) {
  119. case state_idle:
  120. dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
  121. break;
  122. case state_address:
  123. /* Do a pointer write first */
  124. outb(iface->address_byte & ~1, ACBSDA);
  125. iface->state = state_command;
  126. break;
  127. case state_command:
  128. outb(iface->command, ACBSDA);
  129. if (iface->address_byte & 1)
  130. iface->state = state_repeat_start;
  131. else
  132. iface->state = state_write;
  133. break;
  134. case state_repeat_start:
  135. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  136. /* fallthrough */
  137. case state_quick:
  138. if (iface->address_byte & 1) {
  139. if (iface->len == 1)
  140. outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
  141. else
  142. outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
  143. outb(iface->address_byte, ACBSDA);
  144. iface->state = state_read;
  145. } else {
  146. outb(iface->address_byte, ACBSDA);
  147. iface->state = state_write;
  148. }
  149. break;
  150. case state_read:
  151. /* Set ACK if receiving the last byte */
  152. if (iface->len == 1)
  153. outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
  154. else
  155. outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
  156. *iface->ptr++ = inb(ACBSDA);
  157. --iface->len;
  158. if (iface->len == 0) {
  159. iface->result = 0;
  160. iface->state = state_idle;
  161. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  162. }
  163. break;
  164. case state_write:
  165. if (iface->len == 0) {
  166. iface->result = 0;
  167. iface->state = state_idle;
  168. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  169. break;
  170. }
  171. outb(*iface->ptr++, ACBSDA);
  172. --iface->len;
  173. break;
  174. }
  175. return;
  176. error:
  177. dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
  178. scx200_acb_state_name[iface->state]);
  179. iface->state = state_idle;
  180. iface->result = -EIO;
  181. iface->needs_reset = 1;
  182. }
  183. #ifdef POLLED_MODE
  184. static void scx200_acb_poll(struct scx200_acb_iface *iface)
  185. {
  186. u8 status;
  187. unsigned long timeout;
  188. timeout = jiffies + POLL_TIMEOUT;
  189. while (time_before(jiffies, timeout)) {
  190. status = inb(ACBST);
  191. if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
  192. scx200_acb_machine(iface, status);
  193. return;
  194. }
  195. msleep(10);
  196. }
  197. dev_err(&iface->adapter.dev, "timeout in state %s\n",
  198. scx200_acb_state_name[iface->state]);
  199. iface->state = state_idle;
  200. iface->result = -EIO;
  201. iface->needs_reset = 1;
  202. }
  203. #endif /* POLLED_MODE */
  204. static void scx200_acb_reset(struct scx200_acb_iface *iface)
  205. {
  206. /* Disable the ACCESS.bus device and Configure the SCL
  207. frequency: 16 clock cycles */
  208. outb(0x70, ACBCTL2);
  209. /* Polling mode */
  210. outb(0, ACBCTL1);
  211. /* Disable slave address */
  212. outb(0, ACBADDR);
  213. /* Enable the ACCESS.bus device */
  214. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  215. /* Free STALL after START */
  216. outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
  217. /* Send a STOP */
  218. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  219. /* Clear BER, NEGACK and STASTR bits */
  220. outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
  221. /* Clear BB bit */
  222. outb(inb(ACBCST) | ACBCST_BB, ACBCST);
  223. }
  224. static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
  225. u16 address, unsigned short flags,
  226. char rw, u8 command, int size,
  227. union i2c_smbus_data *data)
  228. {
  229. struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
  230. int len;
  231. u8 *buffer;
  232. u16 cur_word;
  233. int rc;
  234. switch (size) {
  235. case I2C_SMBUS_QUICK:
  236. len = 0;
  237. buffer = NULL;
  238. break;
  239. case I2C_SMBUS_BYTE:
  240. len = 1;
  241. buffer = rw ? &data->byte : &command;
  242. break;
  243. case I2C_SMBUS_BYTE_DATA:
  244. len = 1;
  245. buffer = &data->byte;
  246. break;
  247. case I2C_SMBUS_WORD_DATA:
  248. len = 2;
  249. cur_word = cpu_to_le16(data->word);
  250. buffer = (u8 *)&cur_word;
  251. break;
  252. case I2C_SMBUS_BLOCK_DATA:
  253. len = data->block[0];
  254. buffer = &data->block[1];
  255. break;
  256. default:
  257. return -EINVAL;
  258. }
  259. dev_dbg(&adapter->dev,
  260. "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
  261. size, address, command, len, rw);
  262. if (!len && rw == I2C_SMBUS_READ) {
  263. dev_dbg(&adapter->dev, "zero length read\n");
  264. return -EINVAL;
  265. }
  266. down(&iface->sem);
  267. iface->address_byte = (address << 1) | rw;
  268. iface->command = command;
  269. iface->ptr = buffer;
  270. iface->len = len;
  271. iface->result = -EINVAL;
  272. iface->needs_reset = 0;
  273. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  274. if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
  275. iface->state = state_quick;
  276. else
  277. iface->state = state_address;
  278. #ifdef POLLED_MODE
  279. while (iface->state != state_idle)
  280. scx200_acb_poll(iface);
  281. #else /* POLLED_MODE */
  282. #error Interrupt driven mode not implemented
  283. #endif /* POLLED_MODE */
  284. if (iface->needs_reset)
  285. scx200_acb_reset(iface);
  286. rc = iface->result;
  287. up(&iface->sem);
  288. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
  289. data->word = le16_to_cpu(cur_word);
  290. #ifdef DEBUG
  291. dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
  292. if (buffer) {
  293. int i;
  294. printk(" data:");
  295. for (i = 0; i < len; ++i)
  296. printk(" %02x", buffer[i]);
  297. }
  298. printk("\n");
  299. #endif
  300. return rc;
  301. }
  302. static u32 scx200_acb_func(struct i2c_adapter *adapter)
  303. {
  304. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  305. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  306. I2C_FUNC_SMBUS_BLOCK_DATA;
  307. }
  308. /* For now, we only handle combined mode (smbus) */
  309. static struct i2c_algorithm scx200_acb_algorithm = {
  310. .smbus_xfer = scx200_acb_smbus_xfer,
  311. .functionality = scx200_acb_func,
  312. };
  313. static struct scx200_acb_iface *scx200_acb_list;
  314. static DECLARE_MUTEX(scx200_acb_list_mutex);
  315. static int scx200_acb_probe(struct scx200_acb_iface *iface)
  316. {
  317. u8 val;
  318. /* Disable the ACCESS.bus device and Configure the SCL
  319. frequency: 16 clock cycles */
  320. outb(0x70, ACBCTL2);
  321. if (inb(ACBCTL2) != 0x70) {
  322. pr_debug(NAME ": ACBCTL2 readback failed\n");
  323. return -ENXIO;
  324. }
  325. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  326. val = inb(ACBCTL1);
  327. if (val) {
  328. pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
  329. val);
  330. return -ENXIO;
  331. }
  332. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  333. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  334. val = inb(ACBCTL1);
  335. if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
  336. pr_debug(NAME ": enabled, but NMINTE won't be set, "
  337. "ACBCTL1=0x%02x\n", val);
  338. return -ENXIO;
  339. }
  340. return 0;
  341. }
  342. static int __init scx200_acb_create(const char *text, int base, int index)
  343. {
  344. struct scx200_acb_iface *iface;
  345. struct i2c_adapter *adapter;
  346. int rc;
  347. char description[64];
  348. iface = kzalloc(sizeof(*iface), GFP_KERNEL);
  349. if (!iface) {
  350. printk(KERN_ERR NAME ": can't allocate memory\n");
  351. rc = -ENOMEM;
  352. goto errout;
  353. }
  354. adapter = &iface->adapter;
  355. i2c_set_adapdata(adapter, iface);
  356. snprintf(adapter->name, I2C_NAME_SIZE, "%s ACB%d", text, index);
  357. adapter->owner = THIS_MODULE;
  358. adapter->id = I2C_HW_SMBUS_SCX200;
  359. adapter->algo = &scx200_acb_algorithm;
  360. adapter->class = I2C_CLASS_HWMON;
  361. init_MUTEX(&iface->sem);
  362. snprintf(description, sizeof(description), "%s ACCESS.bus [%s]",
  363. text, adapter->name);
  364. if (request_region(base, 8, description) == 0) {
  365. printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n",
  366. base, base + 8-1);
  367. rc = -EBUSY;
  368. goto errout_free;
  369. }
  370. iface->base = base;
  371. rc = scx200_acb_probe(iface);
  372. if (rc) {
  373. printk(KERN_WARNING NAME ": probe failed\n");
  374. goto errout_release;
  375. }
  376. scx200_acb_reset(iface);
  377. if (i2c_add_adapter(adapter) < 0) {
  378. printk(KERN_ERR NAME ": failed to register\n");
  379. rc = -ENODEV;
  380. goto errout_release;
  381. }
  382. down(&scx200_acb_list_mutex);
  383. iface->next = scx200_acb_list;
  384. scx200_acb_list = iface;
  385. up(&scx200_acb_list_mutex);
  386. return 0;
  387. errout_release:
  388. release_region(iface->base, 8);
  389. errout_free:
  390. kfree(iface);
  391. errout:
  392. return rc;
  393. }
  394. static struct pci_device_id scx200[] = {
  395. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
  396. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
  397. { },
  398. };
  399. static struct pci_device_id divil_pci[] = {
  400. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  401. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  402. { } /* NULL entry */
  403. };
  404. #define MSR_LBAR_SMB 0x5140000B
  405. static int scx200_add_cs553x(void)
  406. {
  407. u32 low, hi;
  408. u32 smb_base;
  409. /* Grab & reserve the SMB I/O range */
  410. rdmsr(MSR_LBAR_SMB, low, hi);
  411. /* Check the IO mask and whether SMB is enabled */
  412. if (hi != 0x0000F001) {
  413. printk(KERN_WARNING NAME ": SMBus not enabled\n");
  414. return -ENODEV;
  415. }
  416. /* SMBus IO size is 8 bytes */
  417. smb_base = low & 0x0000FFF8;
  418. return scx200_acb_create("CS5535", smb_base, 0);
  419. }
  420. static int __init scx200_acb_init(void)
  421. {
  422. int i;
  423. int rc = -ENODEV;
  424. pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
  425. /* Verify that this really is a SCx200 processor */
  426. if (pci_dev_present(scx200)) {
  427. for (i = 0; i < MAX_DEVICES; ++i) {
  428. if (base[i] > 0)
  429. rc = scx200_acb_create("SCx200", base[i], i);
  430. }
  431. } else if (pci_dev_present(divil_pci))
  432. rc = scx200_add_cs553x();
  433. return rc;
  434. }
  435. static void __exit scx200_acb_cleanup(void)
  436. {
  437. struct scx200_acb_iface *iface;
  438. down(&scx200_acb_list_mutex);
  439. while ((iface = scx200_acb_list) != NULL) {
  440. scx200_acb_list = iface->next;
  441. up(&scx200_acb_list_mutex);
  442. i2c_del_adapter(&iface->adapter);
  443. release_region(iface->base, 8);
  444. kfree(iface);
  445. down(&scx200_acb_list_mutex);
  446. }
  447. up(&scx200_acb_list_mutex);
  448. }
  449. module_init(scx200_acb_init);
  450. module_exit(scx200_acb_cleanup);