i2c-ismt.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. * The full GNU General Public License is included in this distribution
  18. * in the file called LICENSE.GPL.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * * Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * * Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * * Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. /*
  49. * Supports the SMBus Message Transport (SMT) in the Intel Atom Processor
  50. * S12xx Product Family.
  51. *
  52. * Features supported by this driver:
  53. * Hardware PEC yes
  54. * Block buffer yes
  55. * Block process call transaction no
  56. * Slave mode no
  57. */
  58. #include <linux/module.h>
  59. #include <linux/pci.h>
  60. #include <linux/kernel.h>
  61. #include <linux/stddef.h>
  62. #include <linux/completion.h>
  63. #include <linux/dma-mapping.h>
  64. #include <linux/i2c.h>
  65. #include <linux/acpi.h>
  66. #include <linux/interrupt.h>
  67. #include <linux/io-64-nonatomic-lo-hi.h>
  68. /* PCI Address Constants */
  69. #define SMBBAR 0
  70. /* PCI DIDs for the Intel SMBus Message Transport (SMT) Devices */
  71. #define PCI_DEVICE_ID_INTEL_S1200_SMT0 0x0c59
  72. #define PCI_DEVICE_ID_INTEL_S1200_SMT1 0x0c5a
  73. #define PCI_DEVICE_ID_INTEL_AVOTON_SMT 0x1f15
  74. #define ISMT_DESC_ENTRIES 2 /* number of descriptor entries */
  75. #define ISMT_MAX_RETRIES 3 /* number of SMBus retries to attempt */
  76. /* Hardware Descriptor Constants - Control Field */
  77. #define ISMT_DESC_CWRL 0x01 /* Command/Write Length */
  78. #define ISMT_DESC_BLK 0X04 /* Perform Block Transaction */
  79. #define ISMT_DESC_FAIR 0x08 /* Set fairness flag upon successful arbit. */
  80. #define ISMT_DESC_PEC 0x10 /* Packet Error Code */
  81. #define ISMT_DESC_I2C 0x20 /* I2C Enable */
  82. #define ISMT_DESC_INT 0x40 /* Interrupt */
  83. #define ISMT_DESC_SOE 0x80 /* Stop On Error */
  84. /* Hardware Descriptor Constants - Status Field */
  85. #define ISMT_DESC_SCS 0x01 /* Success */
  86. #define ISMT_DESC_DLTO 0x04 /* Data Low Time Out */
  87. #define ISMT_DESC_NAK 0x08 /* NAK Received */
  88. #define ISMT_DESC_CRC 0x10 /* CRC Error */
  89. #define ISMT_DESC_CLTO 0x20 /* Clock Low Time Out */
  90. #define ISMT_DESC_COL 0x40 /* Collisions */
  91. #define ISMT_DESC_LPR 0x80 /* Large Packet Received */
  92. /* Macros */
  93. #define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
  94. /* iSMT General Register address offsets (SMBBAR + <addr>) */
  95. #define ISMT_GR_GCTRL 0x000 /* General Control */
  96. #define ISMT_GR_SMTICL 0x008 /* SMT Interrupt Cause Location */
  97. #define ISMT_GR_ERRINTMSK 0x010 /* Error Interrupt Mask */
  98. #define ISMT_GR_ERRAERMSK 0x014 /* Error AER Mask */
  99. #define ISMT_GR_ERRSTS 0x018 /* Error Status */
  100. #define ISMT_GR_ERRINFO 0x01c /* Error Information */
  101. /* iSMT Master Registers */
  102. #define ISMT_MSTR_MDBA 0x100 /* Master Descriptor Base Address */
  103. #define ISMT_MSTR_MCTRL 0x108 /* Master Control */
  104. #define ISMT_MSTR_MSTS 0x10c /* Master Status */
  105. #define ISMT_MSTR_MDS 0x110 /* Master Descriptor Size */
  106. #define ISMT_MSTR_RPOLICY 0x114 /* Retry Policy */
  107. /* iSMT Miscellaneous Registers */
  108. #define ISMT_SPGT 0x300 /* SMBus PHY Global Timing */
  109. /* General Control Register (GCTRL) bit definitions */
  110. #define ISMT_GCTRL_TRST 0x04 /* Target Reset */
  111. #define ISMT_GCTRL_KILL 0x08 /* Kill */
  112. #define ISMT_GCTRL_SRST 0x40 /* Soft Reset */
  113. /* Master Control Register (MCTRL) bit definitions */
  114. #define ISMT_MCTRL_SS 0x01 /* Start/Stop */
  115. #define ISMT_MCTRL_MEIE 0x10 /* Master Error Interrupt Enable */
  116. #define ISMT_MCTRL_FMHP 0x00ff0000 /* Firmware Master Head Ptr (FMHP) */
  117. /* Master Status Register (MSTS) bit definitions */
  118. #define ISMT_MSTS_HMTP 0xff0000 /* HW Master Tail Pointer (HMTP) */
  119. #define ISMT_MSTS_MIS 0x20 /* Master Interrupt Status (MIS) */
  120. #define ISMT_MSTS_MEIS 0x10 /* Master Error Int Status (MEIS) */
  121. #define ISMT_MSTS_IP 0x01 /* In Progress */
  122. /* Master Descriptor Size (MDS) bit definitions */
  123. #define ISMT_MDS_MASK 0xff /* Master Descriptor Size mask (MDS) */
  124. /* SMBus PHY Global Timing Register (SPGT) bit definitions */
  125. #define ISMT_SPGT_SPD_MASK 0xc0000000 /* SMBus Speed mask */
  126. #define ISMT_SPGT_SPD_80K 0x00 /* 80 kHz */
  127. #define ISMT_SPGT_SPD_100K (0x1 << 30) /* 100 kHz */
  128. #define ISMT_SPGT_SPD_400K (0x2 << 30) /* 400 kHz */
  129. #define ISMT_SPGT_SPD_1M (0x3 << 30) /* 1 MHz */
  130. /* MSI Control Register (MSICTL) bit definitions */
  131. #define ISMT_MSICTL_MSIE 0x01 /* MSI Enable */
  132. /* iSMT Hardware Descriptor */
  133. struct ismt_desc {
  134. u8 tgtaddr_rw; /* target address & r/w bit */
  135. u8 wr_len_cmd; /* write length in bytes or a command */
  136. u8 rd_len; /* read length */
  137. u8 control; /* control bits */
  138. u8 status; /* status bits */
  139. u8 retry; /* collision retry and retry count */
  140. u8 rxbytes; /* received bytes */
  141. u8 txbytes; /* transmitted bytes */
  142. u32 dptr_low; /* lower 32 bit of the data pointer */
  143. u32 dptr_high; /* upper 32 bit of the data pointer */
  144. } __packed;
  145. struct ismt_priv {
  146. struct i2c_adapter adapter;
  147. void __iomem *smba; /* PCI BAR */
  148. struct pci_dev *pci_dev;
  149. struct ismt_desc *hw; /* descriptor virt base addr */
  150. dma_addr_t io_rng_dma; /* descriptor HW base addr */
  151. u8 head; /* ring buffer head pointer */
  152. struct completion cmp; /* interrupt completion */
  153. u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* temp R/W data buffer */
  154. };
  155. /**
  156. * ismt_ids - PCI device IDs supported by this driver
  157. */
  158. static const struct pci_device_id ismt_ids[] = {
  159. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
  160. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
  161. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMT) },
  162. { 0, }
  163. };
  164. MODULE_DEVICE_TABLE(pci, ismt_ids);
  165. /* Bus speed control bits for slow debuggers - refer to the docs for usage */
  166. static unsigned int bus_speed;
  167. module_param(bus_speed, uint, S_IRUGO);
  168. MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
  169. /**
  170. * __ismt_desc_dump() - dump the contents of a specific descriptor
  171. */
  172. static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
  173. {
  174. dev_dbg(dev, "Descriptor struct: %p\n", desc);
  175. dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
  176. dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
  177. dev_dbg(dev, "\trd_len= 0x%02X\n", desc->rd_len);
  178. dev_dbg(dev, "\tcontrol= 0x%02X\n", desc->control);
  179. dev_dbg(dev, "\tstatus= 0x%02X\n", desc->status);
  180. dev_dbg(dev, "\tretry= 0x%02X\n", desc->retry);
  181. dev_dbg(dev, "\trxbytes= 0x%02X\n", desc->rxbytes);
  182. dev_dbg(dev, "\ttxbytes= 0x%02X\n", desc->txbytes);
  183. dev_dbg(dev, "\tdptr_low= 0x%08X\n", desc->dptr_low);
  184. dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
  185. }
  186. /**
  187. * ismt_desc_dump() - dump the contents of a descriptor for debug purposes
  188. * @priv: iSMT private data
  189. */
  190. static void ismt_desc_dump(struct ismt_priv *priv)
  191. {
  192. struct device *dev = &priv->pci_dev->dev;
  193. struct ismt_desc *desc = &priv->hw[priv->head];
  194. dev_dbg(dev, "Dump of the descriptor struct: 0x%X\n", priv->head);
  195. __ismt_desc_dump(dev, desc);
  196. }
  197. /**
  198. * ismt_gen_reg_dump() - dump the iSMT General Registers
  199. * @priv: iSMT private data
  200. */
  201. static void ismt_gen_reg_dump(struct ismt_priv *priv)
  202. {
  203. struct device *dev = &priv->pci_dev->dev;
  204. dev_dbg(dev, "Dump of the iSMT General Registers\n");
  205. dev_dbg(dev, " GCTRL.... : (0x%p)=0x%X\n",
  206. priv->smba + ISMT_GR_GCTRL,
  207. readl(priv->smba + ISMT_GR_GCTRL));
  208. dev_dbg(dev, " SMTICL... : (0x%p)=0x%016llX\n",
  209. priv->smba + ISMT_GR_SMTICL,
  210. (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
  211. dev_dbg(dev, " ERRINTMSK : (0x%p)=0x%X\n",
  212. priv->smba + ISMT_GR_ERRINTMSK,
  213. readl(priv->smba + ISMT_GR_ERRINTMSK));
  214. dev_dbg(dev, " ERRAERMSK : (0x%p)=0x%X\n",
  215. priv->smba + ISMT_GR_ERRAERMSK,
  216. readl(priv->smba + ISMT_GR_ERRAERMSK));
  217. dev_dbg(dev, " ERRSTS... : (0x%p)=0x%X\n",
  218. priv->smba + ISMT_GR_ERRSTS,
  219. readl(priv->smba + ISMT_GR_ERRSTS));
  220. dev_dbg(dev, " ERRINFO.. : (0x%p)=0x%X\n",
  221. priv->smba + ISMT_GR_ERRINFO,
  222. readl(priv->smba + ISMT_GR_ERRINFO));
  223. }
  224. /**
  225. * ismt_mstr_reg_dump() - dump the iSMT Master Registers
  226. * @priv: iSMT private data
  227. */
  228. static void ismt_mstr_reg_dump(struct ismt_priv *priv)
  229. {
  230. struct device *dev = &priv->pci_dev->dev;
  231. dev_dbg(dev, "Dump of the iSMT Master Registers\n");
  232. dev_dbg(dev, " MDBA..... : (0x%p)=0x%016llX\n",
  233. priv->smba + ISMT_MSTR_MDBA,
  234. (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
  235. dev_dbg(dev, " MCTRL.... : (0x%p)=0x%X\n",
  236. priv->smba + ISMT_MSTR_MCTRL,
  237. readl(priv->smba + ISMT_MSTR_MCTRL));
  238. dev_dbg(dev, " MSTS..... : (0x%p)=0x%X\n",
  239. priv->smba + ISMT_MSTR_MSTS,
  240. readl(priv->smba + ISMT_MSTR_MSTS));
  241. dev_dbg(dev, " MDS...... : (0x%p)=0x%X\n",
  242. priv->smba + ISMT_MSTR_MDS,
  243. readl(priv->smba + ISMT_MSTR_MDS));
  244. dev_dbg(dev, " RPOLICY.. : (0x%p)=0x%X\n",
  245. priv->smba + ISMT_MSTR_RPOLICY,
  246. readl(priv->smba + ISMT_MSTR_RPOLICY));
  247. dev_dbg(dev, " SPGT..... : (0x%p)=0x%X\n",
  248. priv->smba + ISMT_SPGT,
  249. readl(priv->smba + ISMT_SPGT));
  250. }
  251. /**
  252. * ismt_submit_desc() - add a descriptor to the ring
  253. * @priv: iSMT private data
  254. */
  255. static void ismt_submit_desc(struct ismt_priv *priv)
  256. {
  257. uint fmhp;
  258. uint val;
  259. ismt_desc_dump(priv);
  260. ismt_gen_reg_dump(priv);
  261. ismt_mstr_reg_dump(priv);
  262. /* Set the FMHP (Firmware Master Head Pointer)*/
  263. fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
  264. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  265. writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
  266. priv->smba + ISMT_MSTR_MCTRL);
  267. /* Set the start bit */
  268. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  269. writel(val | ISMT_MCTRL_SS,
  270. priv->smba + ISMT_MSTR_MCTRL);
  271. }
  272. /**
  273. * ismt_process_desc() - handle the completion of the descriptor
  274. * @desc: the iSMT hardware descriptor
  275. * @data: data buffer from the upper layer
  276. * @priv: ismt_priv struct holding our dma buffer
  277. * @size: SMBus transaction type
  278. * @read_write: flag to indicate if this is a read or write
  279. */
  280. static int ismt_process_desc(const struct ismt_desc *desc,
  281. union i2c_smbus_data *data,
  282. struct ismt_priv *priv, int size,
  283. char read_write)
  284. {
  285. u8 *dma_buffer = priv->dma_buffer;
  286. dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
  287. __ismt_desc_dump(&priv->pci_dev->dev, desc);
  288. if (desc->status & ISMT_DESC_SCS) {
  289. if (read_write == I2C_SMBUS_WRITE &&
  290. size != I2C_SMBUS_PROC_CALL)
  291. return 0;
  292. switch (size) {
  293. case I2C_SMBUS_BYTE:
  294. case I2C_SMBUS_BYTE_DATA:
  295. data->byte = dma_buffer[0];
  296. break;
  297. case I2C_SMBUS_WORD_DATA:
  298. case I2C_SMBUS_PROC_CALL:
  299. data->word = dma_buffer[0] | (dma_buffer[1] << 8);
  300. break;
  301. case I2C_SMBUS_BLOCK_DATA:
  302. if (desc->rxbytes != dma_buffer[0] + 1)
  303. return -EMSGSIZE;
  304. memcpy(data->block, dma_buffer, desc->rxbytes);
  305. break;
  306. case I2C_SMBUS_I2C_BLOCK_DATA:
  307. memcpy(&data->block[1], dma_buffer, desc->rxbytes);
  308. data->block[0] = desc->rxbytes;
  309. break;
  310. }
  311. return 0;
  312. }
  313. if (likely(desc->status & ISMT_DESC_NAK))
  314. return -ENXIO;
  315. if (desc->status & ISMT_DESC_CRC)
  316. return -EBADMSG;
  317. if (desc->status & ISMT_DESC_COL)
  318. return -EAGAIN;
  319. if (desc->status & ISMT_DESC_LPR)
  320. return -EPROTO;
  321. if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
  322. return -ETIMEDOUT;
  323. return -EIO;
  324. }
  325. /**
  326. * ismt_access() - process an SMBus command
  327. * @adap: the i2c host adapter
  328. * @addr: address of the i2c/SMBus target
  329. * @flags: command options
  330. * @read_write: read from or write to device
  331. * @command: the i2c/SMBus command to issue
  332. * @size: SMBus transaction type
  333. * @data: read/write data buffer
  334. */
  335. static int ismt_access(struct i2c_adapter *adap, u16 addr,
  336. unsigned short flags, char read_write, u8 command,
  337. int size, union i2c_smbus_data *data)
  338. {
  339. int ret;
  340. unsigned long time_left;
  341. dma_addr_t dma_addr = 0; /* address of the data buffer */
  342. u8 dma_size = 0;
  343. enum dma_data_direction dma_direction = 0;
  344. struct ismt_desc *desc;
  345. struct ismt_priv *priv = i2c_get_adapdata(adap);
  346. struct device *dev = &priv->pci_dev->dev;
  347. desc = &priv->hw[priv->head];
  348. /* Initialize the DMA buffer */
  349. memset(priv->dma_buffer, 0, sizeof(priv->dma_buffer));
  350. /* Initialize the descriptor */
  351. memset(desc, 0, sizeof(struct ismt_desc));
  352. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
  353. /* Initialize common control bits */
  354. if (likely(pci_dev_msi_enabled(priv->pci_dev)))
  355. desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
  356. else
  357. desc->control = ISMT_DESC_FAIR;
  358. if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
  359. && (size != I2C_SMBUS_I2C_BLOCK_DATA))
  360. desc->control |= ISMT_DESC_PEC;
  361. switch (size) {
  362. case I2C_SMBUS_QUICK:
  363. dev_dbg(dev, "I2C_SMBUS_QUICK\n");
  364. break;
  365. case I2C_SMBUS_BYTE:
  366. if (read_write == I2C_SMBUS_WRITE) {
  367. /*
  368. * Send Byte
  369. * The command field contains the write data
  370. */
  371. dev_dbg(dev, "I2C_SMBUS_BYTE: WRITE\n");
  372. desc->control |= ISMT_DESC_CWRL;
  373. desc->wr_len_cmd = command;
  374. } else {
  375. /* Receive Byte */
  376. dev_dbg(dev, "I2C_SMBUS_BYTE: READ\n");
  377. dma_size = 1;
  378. dma_direction = DMA_FROM_DEVICE;
  379. desc->rd_len = 1;
  380. }
  381. break;
  382. case I2C_SMBUS_BYTE_DATA:
  383. if (read_write == I2C_SMBUS_WRITE) {
  384. /*
  385. * Write Byte
  386. * Command plus 1 data byte
  387. */
  388. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: WRITE\n");
  389. desc->wr_len_cmd = 2;
  390. dma_size = 2;
  391. dma_direction = DMA_TO_DEVICE;
  392. priv->dma_buffer[0] = command;
  393. priv->dma_buffer[1] = data->byte;
  394. } else {
  395. /* Read Byte */
  396. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: READ\n");
  397. desc->control |= ISMT_DESC_CWRL;
  398. desc->wr_len_cmd = command;
  399. desc->rd_len = 1;
  400. dma_size = 1;
  401. dma_direction = DMA_FROM_DEVICE;
  402. }
  403. break;
  404. case I2C_SMBUS_WORD_DATA:
  405. if (read_write == I2C_SMBUS_WRITE) {
  406. /* Write Word */
  407. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: WRITE\n");
  408. desc->wr_len_cmd = 3;
  409. dma_size = 3;
  410. dma_direction = DMA_TO_DEVICE;
  411. priv->dma_buffer[0] = command;
  412. priv->dma_buffer[1] = data->word & 0xff;
  413. priv->dma_buffer[2] = data->word >> 8;
  414. } else {
  415. /* Read Word */
  416. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: READ\n");
  417. desc->wr_len_cmd = command;
  418. desc->control |= ISMT_DESC_CWRL;
  419. desc->rd_len = 2;
  420. dma_size = 2;
  421. dma_direction = DMA_FROM_DEVICE;
  422. }
  423. break;
  424. case I2C_SMBUS_PROC_CALL:
  425. dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
  426. desc->wr_len_cmd = 3;
  427. desc->rd_len = 2;
  428. dma_size = 3;
  429. dma_direction = DMA_BIDIRECTIONAL;
  430. priv->dma_buffer[0] = command;
  431. priv->dma_buffer[1] = data->word & 0xff;
  432. priv->dma_buffer[2] = data->word >> 8;
  433. break;
  434. case I2C_SMBUS_BLOCK_DATA:
  435. if (read_write == I2C_SMBUS_WRITE) {
  436. /* Block Write */
  437. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: WRITE\n");
  438. dma_size = data->block[0] + 1;
  439. dma_direction = DMA_TO_DEVICE;
  440. desc->wr_len_cmd = dma_size;
  441. desc->control |= ISMT_DESC_BLK;
  442. priv->dma_buffer[0] = command;
  443. memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
  444. } else {
  445. /* Block Read */
  446. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
  447. dma_size = I2C_SMBUS_BLOCK_MAX;
  448. dma_direction = DMA_FROM_DEVICE;
  449. desc->rd_len = dma_size;
  450. desc->wr_len_cmd = command;
  451. desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
  452. }
  453. break;
  454. case I2C_SMBUS_I2C_BLOCK_DATA:
  455. /* Make sure the length is valid */
  456. if (data->block[0] < 1)
  457. data->block[0] = 1;
  458. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  459. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  460. if (read_write == I2C_SMBUS_WRITE) {
  461. /* i2c Block Write */
  462. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: WRITE\n");
  463. dma_size = data->block[0] + 1;
  464. dma_direction = DMA_TO_DEVICE;
  465. desc->wr_len_cmd = dma_size;
  466. desc->control |= ISMT_DESC_I2C;
  467. priv->dma_buffer[0] = command;
  468. memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
  469. } else {
  470. /* i2c Block Read */
  471. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n");
  472. dma_size = data->block[0];
  473. dma_direction = DMA_FROM_DEVICE;
  474. desc->rd_len = dma_size;
  475. desc->wr_len_cmd = command;
  476. desc->control |= (ISMT_DESC_I2C | ISMT_DESC_CWRL);
  477. /*
  478. * Per the "Table 15-15. I2C Commands",
  479. * in the External Design Specification (EDS),
  480. * (Document Number: 508084, Revision: 2.0),
  481. * the _rw bit must be 0
  482. */
  483. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 0);
  484. }
  485. break;
  486. default:
  487. dev_err(dev, "Unsupported transaction %d\n",
  488. size);
  489. return -EOPNOTSUPP;
  490. }
  491. /* map the data buffer */
  492. if (dma_size != 0) {
  493. dev_dbg(dev, " dev=%p\n", dev);
  494. dev_dbg(dev, " data=%p\n", data);
  495. dev_dbg(dev, " dma_buffer=%p\n", priv->dma_buffer);
  496. dev_dbg(dev, " dma_size=%d\n", dma_size);
  497. dev_dbg(dev, " dma_direction=%d\n", dma_direction);
  498. dma_addr = dma_map_single(dev,
  499. priv->dma_buffer,
  500. dma_size,
  501. dma_direction);
  502. if (dma_mapping_error(dev, dma_addr)) {
  503. dev_err(dev, "Error in mapping dma buffer %p\n",
  504. priv->dma_buffer);
  505. return -EIO;
  506. }
  507. dev_dbg(dev, " dma_addr = 0x%016llX\n",
  508. (unsigned long long)dma_addr);
  509. desc->dptr_low = lower_32_bits(dma_addr);
  510. desc->dptr_high = upper_32_bits(dma_addr);
  511. }
  512. reinit_completion(&priv->cmp);
  513. /* Add the descriptor */
  514. ismt_submit_desc(priv);
  515. /* Now we wait for interrupt completion, 1s */
  516. time_left = wait_for_completion_timeout(&priv->cmp, HZ*1);
  517. /* unmap the data buffer */
  518. if (dma_size != 0)
  519. dma_unmap_single(dev, dma_addr, dma_size, dma_direction);
  520. if (unlikely(!time_left)) {
  521. dev_err(dev, "completion wait timed out\n");
  522. ret = -ETIMEDOUT;
  523. goto out;
  524. }
  525. /* do any post processing of the descriptor here */
  526. ret = ismt_process_desc(desc, data, priv, size, read_write);
  527. out:
  528. /* Update the ring pointer */
  529. priv->head++;
  530. priv->head %= ISMT_DESC_ENTRIES;
  531. return ret;
  532. }
  533. /**
  534. * ismt_func() - report which i2c commands are supported by this adapter
  535. * @adap: the i2c host adapter
  536. */
  537. static u32 ismt_func(struct i2c_adapter *adap)
  538. {
  539. return I2C_FUNC_SMBUS_QUICK |
  540. I2C_FUNC_SMBUS_BYTE |
  541. I2C_FUNC_SMBUS_BYTE_DATA |
  542. I2C_FUNC_SMBUS_WORD_DATA |
  543. I2C_FUNC_SMBUS_PROC_CALL |
  544. I2C_FUNC_SMBUS_BLOCK_DATA |
  545. I2C_FUNC_SMBUS_I2C_BLOCK |
  546. I2C_FUNC_SMBUS_PEC;
  547. }
  548. /**
  549. * smbus_algorithm - the adapter algorithm and supported functionality
  550. * @smbus_xfer: the adapter algorithm
  551. * @functionality: functionality supported by the adapter
  552. */
  553. static const struct i2c_algorithm smbus_algorithm = {
  554. .smbus_xfer = ismt_access,
  555. .functionality = ismt_func,
  556. };
  557. /**
  558. * ismt_handle_isr() - interrupt handler bottom half
  559. * @priv: iSMT private data
  560. */
  561. static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
  562. {
  563. complete(&priv->cmp);
  564. return IRQ_HANDLED;
  565. }
  566. /**
  567. * ismt_do_interrupt() - IRQ interrupt handler
  568. * @vec: interrupt vector
  569. * @data: iSMT private data
  570. */
  571. static irqreturn_t ismt_do_interrupt(int vec, void *data)
  572. {
  573. u32 val;
  574. struct ismt_priv *priv = data;
  575. /*
  576. * check to see it's our interrupt, return IRQ_NONE if not ours
  577. * since we are sharing interrupt
  578. */
  579. val = readl(priv->smba + ISMT_MSTR_MSTS);
  580. if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
  581. return IRQ_NONE;
  582. else
  583. writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
  584. priv->smba + ISMT_MSTR_MSTS);
  585. return ismt_handle_isr(priv);
  586. }
  587. /**
  588. * ismt_do_msi_interrupt() - MSI interrupt handler
  589. * @vec: interrupt vector
  590. * @data: iSMT private data
  591. */
  592. static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
  593. {
  594. return ismt_handle_isr(data);
  595. }
  596. /**
  597. * ismt_hw_init() - initialize the iSMT hardware
  598. * @priv: iSMT private data
  599. */
  600. static void ismt_hw_init(struct ismt_priv *priv)
  601. {
  602. u32 val;
  603. struct device *dev = &priv->pci_dev->dev;
  604. /* initialize the Master Descriptor Base Address (MDBA) */
  605. writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
  606. /* initialize the Master Control Register (MCTRL) */
  607. writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
  608. /* initialize the Master Status Register (MSTS) */
  609. writel(0, priv->smba + ISMT_MSTR_MSTS);
  610. /* initialize the Master Descriptor Size (MDS) */
  611. val = readl(priv->smba + ISMT_MSTR_MDS);
  612. writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
  613. priv->smba + ISMT_MSTR_MDS);
  614. /*
  615. * Set the SMBus speed (could use this for slow HW debuggers)
  616. */
  617. val = readl(priv->smba + ISMT_SPGT);
  618. switch (bus_speed) {
  619. case 0:
  620. break;
  621. case 80:
  622. dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
  623. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
  624. priv->smba + ISMT_SPGT);
  625. break;
  626. case 100:
  627. dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
  628. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
  629. priv->smba + ISMT_SPGT);
  630. break;
  631. case 400:
  632. dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
  633. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
  634. priv->smba + ISMT_SPGT);
  635. break;
  636. case 1000:
  637. dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
  638. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
  639. priv->smba + ISMT_SPGT);
  640. break;
  641. default:
  642. dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
  643. break;
  644. }
  645. val = readl(priv->smba + ISMT_SPGT);
  646. switch (val & ISMT_SPGT_SPD_MASK) {
  647. case ISMT_SPGT_SPD_80K:
  648. bus_speed = 80;
  649. break;
  650. case ISMT_SPGT_SPD_100K:
  651. bus_speed = 100;
  652. break;
  653. case ISMT_SPGT_SPD_400K:
  654. bus_speed = 400;
  655. break;
  656. case ISMT_SPGT_SPD_1M:
  657. bus_speed = 1000;
  658. break;
  659. }
  660. dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
  661. }
  662. /**
  663. * ismt_dev_init() - initialize the iSMT data structures
  664. * @priv: iSMT private data
  665. */
  666. static int ismt_dev_init(struct ismt_priv *priv)
  667. {
  668. /* allocate memory for the descriptor */
  669. priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
  670. (ISMT_DESC_ENTRIES
  671. * sizeof(struct ismt_desc)),
  672. &priv->io_rng_dma,
  673. GFP_KERNEL);
  674. if (!priv->hw)
  675. return -ENOMEM;
  676. memset(priv->hw, 0, (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc)));
  677. priv->head = 0;
  678. init_completion(&priv->cmp);
  679. return 0;
  680. }
  681. /**
  682. * ismt_int_init() - initialize interrupts
  683. * @priv: iSMT private data
  684. */
  685. static int ismt_int_init(struct ismt_priv *priv)
  686. {
  687. int err;
  688. /* Try using MSI interrupts */
  689. err = pci_enable_msi(priv->pci_dev);
  690. if (err)
  691. goto intx;
  692. err = devm_request_irq(&priv->pci_dev->dev,
  693. priv->pci_dev->irq,
  694. ismt_do_msi_interrupt,
  695. 0,
  696. "ismt-msi",
  697. priv);
  698. if (err) {
  699. pci_disable_msi(priv->pci_dev);
  700. goto intx;
  701. }
  702. return 0;
  703. /* Try using legacy interrupts */
  704. intx:
  705. dev_warn(&priv->pci_dev->dev,
  706. "Unable to use MSI interrupts, falling back to legacy\n");
  707. err = devm_request_irq(&priv->pci_dev->dev,
  708. priv->pci_dev->irq,
  709. ismt_do_interrupt,
  710. IRQF_SHARED,
  711. "ismt-intx",
  712. priv);
  713. if (err) {
  714. dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
  715. return err;
  716. }
  717. return 0;
  718. }
  719. static struct pci_driver ismt_driver;
  720. /**
  721. * ismt_probe() - probe for iSMT devices
  722. * @pdev: PCI-Express device
  723. * @id: PCI-Express device ID
  724. */
  725. static int
  726. ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  727. {
  728. int err;
  729. struct ismt_priv *priv;
  730. unsigned long start, len;
  731. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  732. if (!priv)
  733. return -ENOMEM;
  734. pci_set_drvdata(pdev, priv);
  735. i2c_set_adapdata(&priv->adapter, priv);
  736. priv->adapter.owner = THIS_MODULE;
  737. priv->adapter.class = I2C_CLASS_HWMON;
  738. priv->adapter.algo = &smbus_algorithm;
  739. priv->adapter.dev.parent = &pdev->dev;
  740. ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev));
  741. priv->adapter.retries = ISMT_MAX_RETRIES;
  742. priv->pci_dev = pdev;
  743. err = pcim_enable_device(pdev);
  744. if (err) {
  745. dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
  746. err);
  747. return err;
  748. }
  749. /* enable bus mastering */
  750. pci_set_master(pdev);
  751. /* Determine the address of the SMBus area */
  752. start = pci_resource_start(pdev, SMBBAR);
  753. len = pci_resource_len(pdev, SMBBAR);
  754. if (!start || !len) {
  755. dev_err(&pdev->dev,
  756. "SMBus base address uninitialized, upgrade BIOS\n");
  757. return -ENODEV;
  758. }
  759. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  760. "SMBus iSMT adapter at %lx", start);
  761. dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
  762. dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
  763. err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
  764. if (err) {
  765. dev_err(&pdev->dev, "ACPI resource conflict!\n");
  766. return err;
  767. }
  768. err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
  769. if (err) {
  770. dev_err(&pdev->dev,
  771. "Failed to request SMBus region 0x%lx-0x%lx\n",
  772. start, start + len);
  773. return err;
  774. }
  775. priv->smba = pcim_iomap(pdev, SMBBAR, len);
  776. if (!priv->smba) {
  777. dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
  778. return -ENODEV;
  779. }
  780. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
  781. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
  782. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  783. (pci_set_consistent_dma_mask(pdev,
  784. DMA_BIT_MASK(32)) != 0)) {
  785. dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
  786. pdev);
  787. return -ENODEV;
  788. }
  789. }
  790. err = ismt_dev_init(priv);
  791. if (err)
  792. return err;
  793. ismt_hw_init(priv);
  794. err = ismt_int_init(priv);
  795. if (err)
  796. return err;
  797. err = i2c_add_adapter(&priv->adapter);
  798. if (err) {
  799. dev_err(&pdev->dev, "Failed to add SMBus iSMT adapter\n");
  800. return -ENODEV;
  801. }
  802. return 0;
  803. }
  804. /**
  805. * ismt_remove() - release driver resources
  806. * @pdev: PCI-Express device
  807. */
  808. static void ismt_remove(struct pci_dev *pdev)
  809. {
  810. struct ismt_priv *priv = pci_get_drvdata(pdev);
  811. i2c_del_adapter(&priv->adapter);
  812. }
  813. static struct pci_driver ismt_driver = {
  814. .name = "ismt_smbus",
  815. .id_table = ismt_ids,
  816. .probe = ismt_probe,
  817. .remove = ismt_remove,
  818. };
  819. module_pci_driver(ismt_driver);
  820. MODULE_LICENSE("Dual BSD/GPL");
  821. MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
  822. MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");