scx200_acb.c 12 KB

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