i2c-ismt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 <asm-generic/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 *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. bool using_msi; /* type of interrupt flag */
  155. };
  156. /**
  157. * ismt_ids - PCI device IDs supported by this driver
  158. */
  159. static const struct pci_device_id ismt_ids[] = {
  160. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
  161. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
  162. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMT) },
  163. { 0, }
  164. };
  165. MODULE_DEVICE_TABLE(pci, ismt_ids);
  166. /* Bus speed control bits for slow debuggers - refer to the docs for usage */
  167. static unsigned int bus_speed;
  168. module_param(bus_speed, uint, S_IRUGO);
  169. MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
  170. /**
  171. * __ismt_desc_dump() - dump the contents of a specific descriptor
  172. */
  173. static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
  174. {
  175. dev_dbg(dev, "Descriptor struct: %p\n", desc);
  176. dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
  177. dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
  178. dev_dbg(dev, "\trd_len= 0x%02X\n", desc->rd_len);
  179. dev_dbg(dev, "\tcontrol= 0x%02X\n", desc->control);
  180. dev_dbg(dev, "\tstatus= 0x%02X\n", desc->status);
  181. dev_dbg(dev, "\tretry= 0x%02X\n", desc->retry);
  182. dev_dbg(dev, "\trxbytes= 0x%02X\n", desc->rxbytes);
  183. dev_dbg(dev, "\ttxbytes= 0x%02X\n", desc->txbytes);
  184. dev_dbg(dev, "\tdptr_low= 0x%08X\n", desc->dptr_low);
  185. dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
  186. }
  187. /**
  188. * ismt_desc_dump() - dump the contents of a descriptor for debug purposes
  189. * @priv: iSMT private data
  190. */
  191. static void ismt_desc_dump(struct ismt_priv *priv)
  192. {
  193. struct device *dev = &priv->pci_dev->dev;
  194. struct ismt_desc *desc = &priv->hw[priv->head];
  195. dev_dbg(dev, "Dump of the descriptor struct: 0x%X\n", priv->head);
  196. __ismt_desc_dump(dev, desc);
  197. }
  198. /**
  199. * ismt_gen_reg_dump() - dump the iSMT General Registers
  200. * @priv: iSMT private data
  201. */
  202. static void ismt_gen_reg_dump(struct ismt_priv *priv)
  203. {
  204. struct device *dev = &priv->pci_dev->dev;
  205. dev_dbg(dev, "Dump of the iSMT General Registers\n");
  206. dev_dbg(dev, " GCTRL.... : (0x%p)=0x%X\n",
  207. priv->smba + ISMT_GR_GCTRL,
  208. readl(priv->smba + ISMT_GR_GCTRL));
  209. dev_dbg(dev, " SMTICL... : (0x%p)=0x%016llX\n",
  210. priv->smba + ISMT_GR_SMTICL,
  211. (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
  212. dev_dbg(dev, " ERRINTMSK : (0x%p)=0x%X\n",
  213. priv->smba + ISMT_GR_ERRINTMSK,
  214. readl(priv->smba + ISMT_GR_ERRINTMSK));
  215. dev_dbg(dev, " ERRAERMSK : (0x%p)=0x%X\n",
  216. priv->smba + ISMT_GR_ERRAERMSK,
  217. readl(priv->smba + ISMT_GR_ERRAERMSK));
  218. dev_dbg(dev, " ERRSTS... : (0x%p)=0x%X\n",
  219. priv->smba + ISMT_GR_ERRSTS,
  220. readl(priv->smba + ISMT_GR_ERRSTS));
  221. dev_dbg(dev, " ERRINFO.. : (0x%p)=0x%X\n",
  222. priv->smba + ISMT_GR_ERRINFO,
  223. readl(priv->smba + ISMT_GR_ERRINFO));
  224. }
  225. /**
  226. * ismt_mstr_reg_dump() - dump the iSMT Master Registers
  227. * @priv: iSMT private data
  228. */
  229. static void ismt_mstr_reg_dump(struct ismt_priv *priv)
  230. {
  231. struct device *dev = &priv->pci_dev->dev;
  232. dev_dbg(dev, "Dump of the iSMT Master Registers\n");
  233. dev_dbg(dev, " MDBA..... : (0x%p)=0x%016llX\n",
  234. priv->smba + ISMT_MSTR_MDBA,
  235. (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
  236. dev_dbg(dev, " MCTRL.... : (0x%p)=0x%X\n",
  237. priv->smba + ISMT_MSTR_MCTRL,
  238. readl(priv->smba + ISMT_MSTR_MCTRL));
  239. dev_dbg(dev, " MSTS..... : (0x%p)=0x%X\n",
  240. priv->smba + ISMT_MSTR_MSTS,
  241. readl(priv->smba + ISMT_MSTR_MSTS));
  242. dev_dbg(dev, " MDS...... : (0x%p)=0x%X\n",
  243. priv->smba + ISMT_MSTR_MDS,
  244. readl(priv->smba + ISMT_MSTR_MDS));
  245. dev_dbg(dev, " RPOLICY.. : (0x%p)=0x%X\n",
  246. priv->smba + ISMT_MSTR_RPOLICY,
  247. readl(priv->smba + ISMT_MSTR_RPOLICY));
  248. dev_dbg(dev, " SPGT..... : (0x%p)=0x%X\n",
  249. priv->smba + ISMT_SPGT,
  250. readl(priv->smba + ISMT_SPGT));
  251. }
  252. /**
  253. * ismt_submit_desc() - add a descriptor to the ring
  254. * @priv: iSMT private data
  255. */
  256. static void ismt_submit_desc(struct ismt_priv *priv)
  257. {
  258. uint fmhp;
  259. uint val;
  260. ismt_desc_dump(priv);
  261. ismt_gen_reg_dump(priv);
  262. ismt_mstr_reg_dump(priv);
  263. /* Set the FMHP (Firmware Master Head Pointer)*/
  264. fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
  265. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  266. writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
  267. priv->smba + ISMT_MSTR_MCTRL);
  268. /* Set the start bit */
  269. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  270. writel(val | ISMT_MCTRL_SS,
  271. priv->smba + ISMT_MSTR_MCTRL);
  272. }
  273. /**
  274. * ismt_process_desc() - handle the completion of the descriptor
  275. * @desc: the iSMT hardware descriptor
  276. * @data: data buffer from the upper layer
  277. * @priv: ismt_priv struct holding our dma buffer
  278. * @size: SMBus transaction type
  279. * @read_write: flag to indicate if this is a read or write
  280. */
  281. static int ismt_process_desc(const struct ismt_desc *desc,
  282. union i2c_smbus_data *data,
  283. struct ismt_priv *priv, int size,
  284. char read_write)
  285. {
  286. u8 *dma_buffer = priv->dma_buffer;
  287. dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
  288. __ismt_desc_dump(&priv->pci_dev->dev, desc);
  289. if (desc->status & ISMT_DESC_SCS) {
  290. if (read_write == I2C_SMBUS_WRITE &&
  291. size != I2C_SMBUS_PROC_CALL)
  292. return 0;
  293. switch (size) {
  294. case I2C_SMBUS_BYTE:
  295. case I2C_SMBUS_BYTE_DATA:
  296. data->byte = dma_buffer[0];
  297. break;
  298. case I2C_SMBUS_WORD_DATA:
  299. case I2C_SMBUS_PROC_CALL:
  300. data->word = dma_buffer[0] | (dma_buffer[1] << 8);
  301. break;
  302. case I2C_SMBUS_BLOCK_DATA:
  303. case I2C_SMBUS_I2C_BLOCK_DATA:
  304. memcpy(&data->block[1], dma_buffer, desc->rxbytes);
  305. data->block[0] = desc->rxbytes;
  306. break;
  307. }
  308. return 0;
  309. }
  310. if (likely(desc->status & ISMT_DESC_NAK))
  311. return -ENXIO;
  312. if (desc->status & ISMT_DESC_CRC)
  313. return -EBADMSG;
  314. if (desc->status & ISMT_DESC_COL)
  315. return -EAGAIN;
  316. if (desc->status & ISMT_DESC_LPR)
  317. return -EPROTO;
  318. if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
  319. return -ETIMEDOUT;
  320. return -EIO;
  321. }
  322. /**
  323. * ismt_access() - process an SMBus command
  324. * @adap: the i2c host adapter
  325. * @addr: address of the i2c/SMBus target
  326. * @flags: command options
  327. * @read_write: read from or write to device
  328. * @command: the i2c/SMBus command to issue
  329. * @size: SMBus transaction type
  330. * @data: read/write data buffer
  331. */
  332. static int ismt_access(struct i2c_adapter *adap, u16 addr,
  333. unsigned short flags, char read_write, u8 command,
  334. int size, union i2c_smbus_data *data)
  335. {
  336. int ret;
  337. unsigned long time_left;
  338. dma_addr_t dma_addr = 0; /* address of the data buffer */
  339. u8 dma_size = 0;
  340. enum dma_data_direction dma_direction = 0;
  341. struct ismt_desc *desc;
  342. struct ismt_priv *priv = i2c_get_adapdata(adap);
  343. struct device *dev = &priv->pci_dev->dev;
  344. desc = &priv->hw[priv->head];
  345. /* Initialize the DMA buffer */
  346. memset(priv->dma_buffer, 0, sizeof(priv->dma_buffer));
  347. /* Initialize the descriptor */
  348. memset(desc, 0, sizeof(struct ismt_desc));
  349. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
  350. /* Initialize common control bits */
  351. if (likely(priv->using_msi))
  352. desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
  353. else
  354. desc->control = ISMT_DESC_FAIR;
  355. if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
  356. && (size != I2C_SMBUS_I2C_BLOCK_DATA))
  357. desc->control |= ISMT_DESC_PEC;
  358. switch (size) {
  359. case I2C_SMBUS_QUICK:
  360. dev_dbg(dev, "I2C_SMBUS_QUICK\n");
  361. break;
  362. case I2C_SMBUS_BYTE:
  363. if (read_write == I2C_SMBUS_WRITE) {
  364. /*
  365. * Send Byte
  366. * The command field contains the write data
  367. */
  368. dev_dbg(dev, "I2C_SMBUS_BYTE: WRITE\n");
  369. desc->control |= ISMT_DESC_CWRL;
  370. desc->wr_len_cmd = command;
  371. } else {
  372. /* Receive Byte */
  373. dev_dbg(dev, "I2C_SMBUS_BYTE: READ\n");
  374. dma_size = 1;
  375. dma_direction = DMA_FROM_DEVICE;
  376. desc->rd_len = 1;
  377. }
  378. break;
  379. case I2C_SMBUS_BYTE_DATA:
  380. if (read_write == I2C_SMBUS_WRITE) {
  381. /*
  382. * Write Byte
  383. * Command plus 1 data byte
  384. */
  385. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: WRITE\n");
  386. desc->wr_len_cmd = 2;
  387. dma_size = 2;
  388. dma_direction = DMA_TO_DEVICE;
  389. priv->dma_buffer[0] = command;
  390. priv->dma_buffer[1] = data->byte;
  391. } else {
  392. /* Read Byte */
  393. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: READ\n");
  394. desc->control |= ISMT_DESC_CWRL;
  395. desc->wr_len_cmd = command;
  396. desc->rd_len = 1;
  397. dma_size = 1;
  398. dma_direction = DMA_FROM_DEVICE;
  399. }
  400. break;
  401. case I2C_SMBUS_WORD_DATA:
  402. if (read_write == I2C_SMBUS_WRITE) {
  403. /* Write Word */
  404. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: WRITE\n");
  405. desc->wr_len_cmd = 3;
  406. dma_size = 3;
  407. dma_direction = DMA_TO_DEVICE;
  408. priv->dma_buffer[0] = command;
  409. priv->dma_buffer[1] = data->word & 0xff;
  410. priv->dma_buffer[2] = data->word >> 8;
  411. } else {
  412. /* Read Word */
  413. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: READ\n");
  414. desc->wr_len_cmd = command;
  415. desc->control |= ISMT_DESC_CWRL;
  416. desc->rd_len = 2;
  417. dma_size = 2;
  418. dma_direction = DMA_FROM_DEVICE;
  419. }
  420. break;
  421. case I2C_SMBUS_PROC_CALL:
  422. dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
  423. desc->wr_len_cmd = 3;
  424. desc->rd_len = 2;
  425. dma_size = 3;
  426. dma_direction = DMA_BIDIRECTIONAL;
  427. priv->dma_buffer[0] = command;
  428. priv->dma_buffer[1] = data->word & 0xff;
  429. priv->dma_buffer[2] = data->word >> 8;
  430. break;
  431. case I2C_SMBUS_BLOCK_DATA:
  432. if (read_write == I2C_SMBUS_WRITE) {
  433. /* Block Write */
  434. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: WRITE\n");
  435. dma_size = data->block[0] + 1;
  436. dma_direction = DMA_TO_DEVICE;
  437. desc->wr_len_cmd = dma_size;
  438. desc->control |= ISMT_DESC_BLK;
  439. priv->dma_buffer[0] = command;
  440. memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
  441. } else {
  442. /* Block Read */
  443. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
  444. dma_size = I2C_SMBUS_BLOCK_MAX;
  445. dma_direction = DMA_FROM_DEVICE;
  446. desc->rd_len = dma_size;
  447. desc->wr_len_cmd = command;
  448. desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
  449. }
  450. break;
  451. case I2C_SMBUS_I2C_BLOCK_DATA:
  452. /* Make sure the length is valid */
  453. if (data->block[0] < 1)
  454. data->block[0] = 1;
  455. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  456. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  457. if (read_write == I2C_SMBUS_WRITE) {
  458. /* i2c Block Write */
  459. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: WRITE\n");
  460. dma_size = data->block[0] + 1;
  461. dma_direction = DMA_TO_DEVICE;
  462. desc->wr_len_cmd = dma_size;
  463. desc->control |= ISMT_DESC_I2C;
  464. priv->dma_buffer[0] = command;
  465. memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
  466. } else {
  467. /* i2c Block Read */
  468. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n");
  469. dma_size = data->block[0];
  470. dma_direction = DMA_FROM_DEVICE;
  471. desc->rd_len = dma_size;
  472. desc->wr_len_cmd = command;
  473. desc->control |= (ISMT_DESC_I2C | ISMT_DESC_CWRL);
  474. /*
  475. * Per the "Table 15-15. I2C Commands",
  476. * in the External Design Specification (EDS),
  477. * (Document Number: 508084, Revision: 2.0),
  478. * the _rw bit must be 0
  479. */
  480. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 0);
  481. }
  482. break;
  483. default:
  484. dev_err(dev, "Unsupported transaction %d\n",
  485. size);
  486. return -EOPNOTSUPP;
  487. }
  488. /* map the data buffer */
  489. if (dma_size != 0) {
  490. dev_dbg(dev, " dev=%p\n", dev);
  491. dev_dbg(dev, " data=%p\n", data);
  492. dev_dbg(dev, " dma_buffer=%p\n", priv->dma_buffer);
  493. dev_dbg(dev, " dma_size=%d\n", dma_size);
  494. dev_dbg(dev, " dma_direction=%d\n", dma_direction);
  495. dma_addr = dma_map_single(dev,
  496. priv->dma_buffer,
  497. dma_size,
  498. dma_direction);
  499. if (dma_mapping_error(dev, dma_addr)) {
  500. dev_err(dev, "Error in mapping dma buffer %p\n",
  501. priv->dma_buffer);
  502. return -EIO;
  503. }
  504. dev_dbg(dev, " dma_addr = 0x%016llX\n",
  505. (unsigned long long)dma_addr);
  506. desc->dptr_low = lower_32_bits(dma_addr);
  507. desc->dptr_high = upper_32_bits(dma_addr);
  508. }
  509. reinit_completion(&priv->cmp);
  510. /* Add the descriptor */
  511. ismt_submit_desc(priv);
  512. /* Now we wait for interrupt completion, 1s */
  513. time_left = wait_for_completion_timeout(&priv->cmp, HZ*1);
  514. /* unmap the data buffer */
  515. if (dma_size != 0)
  516. dma_unmap_single(&adap->dev, dma_addr, dma_size, dma_direction);
  517. if (unlikely(!time_left)) {
  518. dev_err(dev, "completion wait timed out\n");
  519. ret = -ETIMEDOUT;
  520. goto out;
  521. }
  522. /* do any post processing of the descriptor here */
  523. ret = ismt_process_desc(desc, data, priv, size, read_write);
  524. out:
  525. /* Update the ring pointer */
  526. priv->head++;
  527. priv->head %= ISMT_DESC_ENTRIES;
  528. return ret;
  529. }
  530. /**
  531. * ismt_func() - report which i2c commands are supported by this adapter
  532. * @adap: the i2c host adapter
  533. */
  534. static u32 ismt_func(struct i2c_adapter *adap)
  535. {
  536. return I2C_FUNC_SMBUS_QUICK |
  537. I2C_FUNC_SMBUS_BYTE |
  538. I2C_FUNC_SMBUS_BYTE_DATA |
  539. I2C_FUNC_SMBUS_WORD_DATA |
  540. I2C_FUNC_SMBUS_PROC_CALL |
  541. I2C_FUNC_SMBUS_BLOCK_DATA |
  542. I2C_FUNC_SMBUS_I2C_BLOCK |
  543. I2C_FUNC_SMBUS_PEC;
  544. }
  545. /**
  546. * smbus_algorithm - the adapter algorithm and supported functionality
  547. * @smbus_xfer: the adapter algorithm
  548. * @functionality: functionality supported by the adapter
  549. */
  550. static const struct i2c_algorithm smbus_algorithm = {
  551. .smbus_xfer = ismt_access,
  552. .functionality = ismt_func,
  553. };
  554. /**
  555. * ismt_handle_isr() - interrupt handler bottom half
  556. * @priv: iSMT private data
  557. */
  558. static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
  559. {
  560. complete(&priv->cmp);
  561. return IRQ_HANDLED;
  562. }
  563. /**
  564. * ismt_do_interrupt() - IRQ interrupt handler
  565. * @vec: interrupt vector
  566. * @data: iSMT private data
  567. */
  568. static irqreturn_t ismt_do_interrupt(int vec, void *data)
  569. {
  570. u32 val;
  571. struct ismt_priv *priv = data;
  572. /*
  573. * check to see it's our interrupt, return IRQ_NONE if not ours
  574. * since we are sharing interrupt
  575. */
  576. val = readl(priv->smba + ISMT_MSTR_MSTS);
  577. if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
  578. return IRQ_NONE;
  579. else
  580. writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
  581. priv->smba + ISMT_MSTR_MSTS);
  582. return ismt_handle_isr(priv);
  583. }
  584. /**
  585. * ismt_do_msi_interrupt() - MSI interrupt handler
  586. * @vec: interrupt vector
  587. * @data: iSMT private data
  588. */
  589. static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
  590. {
  591. return ismt_handle_isr(data);
  592. }
  593. /**
  594. * ismt_hw_init() - initialize the iSMT hardware
  595. * @priv: iSMT private data
  596. */
  597. static void ismt_hw_init(struct ismt_priv *priv)
  598. {
  599. u32 val;
  600. struct device *dev = &priv->pci_dev->dev;
  601. /* initialize the Master Descriptor Base Address (MDBA) */
  602. writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
  603. /* initialize the Master Control Register (MCTRL) */
  604. writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
  605. /* initialize the Master Status Register (MSTS) */
  606. writel(0, priv->smba + ISMT_MSTR_MSTS);
  607. /* initialize the Master Descriptor Size (MDS) */
  608. val = readl(priv->smba + ISMT_MSTR_MDS);
  609. writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
  610. priv->smba + ISMT_MSTR_MDS);
  611. /*
  612. * Set the SMBus speed (could use this for slow HW debuggers)
  613. */
  614. val = readl(priv->smba + ISMT_SPGT);
  615. switch (bus_speed) {
  616. case 0:
  617. break;
  618. case 80:
  619. dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
  620. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
  621. priv->smba + ISMT_SPGT);
  622. break;
  623. case 100:
  624. dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
  625. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
  626. priv->smba + ISMT_SPGT);
  627. break;
  628. case 400:
  629. dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
  630. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
  631. priv->smba + ISMT_SPGT);
  632. break;
  633. case 1000:
  634. dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
  635. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
  636. priv->smba + ISMT_SPGT);
  637. break;
  638. default:
  639. dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
  640. break;
  641. }
  642. val = readl(priv->smba + ISMT_SPGT);
  643. switch (val & ISMT_SPGT_SPD_MASK) {
  644. case ISMT_SPGT_SPD_80K:
  645. bus_speed = 80;
  646. break;
  647. case ISMT_SPGT_SPD_100K:
  648. bus_speed = 100;
  649. break;
  650. case ISMT_SPGT_SPD_400K:
  651. bus_speed = 400;
  652. break;
  653. case ISMT_SPGT_SPD_1M:
  654. bus_speed = 1000;
  655. break;
  656. }
  657. dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
  658. }
  659. /**
  660. * ismt_dev_init() - initialize the iSMT data structures
  661. * @priv: iSMT private data
  662. */
  663. static int ismt_dev_init(struct ismt_priv *priv)
  664. {
  665. /* allocate memory for the descriptor */
  666. priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
  667. (ISMT_DESC_ENTRIES
  668. * sizeof(struct ismt_desc)),
  669. &priv->io_rng_dma,
  670. GFP_KERNEL);
  671. if (!priv->hw)
  672. return -ENOMEM;
  673. memset(priv->hw, 0, (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc)));
  674. priv->head = 0;
  675. init_completion(&priv->cmp);
  676. return 0;
  677. }
  678. /**
  679. * ismt_int_init() - initialize interrupts
  680. * @priv: iSMT private data
  681. */
  682. static int ismt_int_init(struct ismt_priv *priv)
  683. {
  684. int err;
  685. /* Try using MSI interrupts */
  686. err = pci_enable_msi(priv->pci_dev);
  687. if (err) {
  688. dev_warn(&priv->pci_dev->dev,
  689. "Unable to use MSI interrupts, falling back to legacy\n");
  690. goto intx;
  691. }
  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. priv->using_msi = true;
  703. goto done;
  704. /* Try using legacy interrupts */
  705. intx:
  706. err = devm_request_irq(&priv->pci_dev->dev,
  707. priv->pci_dev->irq,
  708. ismt_do_interrupt,
  709. IRQF_SHARED,
  710. "ismt-intx",
  711. priv);
  712. if (err) {
  713. dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
  714. return -ENODEV;
  715. }
  716. priv->using_msi = false;
  717. done:
  718. return 0;
  719. }
  720. static struct pci_driver ismt_driver;
  721. /**
  722. * ismt_probe() - probe for iSMT devices
  723. * @pdev: PCI-Express device
  724. * @id: PCI-Express device ID
  725. */
  726. static int
  727. ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  728. {
  729. int err;
  730. struct ismt_priv *priv;
  731. unsigned long start, len;
  732. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  733. if (!priv)
  734. return -ENOMEM;
  735. pci_set_drvdata(pdev, priv);
  736. i2c_set_adapdata(&priv->adapter, priv);
  737. priv->adapter.owner = THIS_MODULE;
  738. priv->adapter.class = I2C_CLASS_HWMON;
  739. priv->adapter.algo = &smbus_algorithm;
  740. /* set up the sysfs linkage to our parent device */
  741. priv->adapter.dev.parent = &pdev->dev;
  742. /* number of retries on lost arbitration */
  743. priv->adapter.retries = ISMT_MAX_RETRIES;
  744. priv->pci_dev = pdev;
  745. err = pcim_enable_device(pdev);
  746. if (err) {
  747. dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
  748. err);
  749. return err;
  750. }
  751. /* enable bus mastering */
  752. pci_set_master(pdev);
  753. /* Determine the address of the SMBus area */
  754. start = pci_resource_start(pdev, SMBBAR);
  755. len = pci_resource_len(pdev, SMBBAR);
  756. if (!start || !len) {
  757. dev_err(&pdev->dev,
  758. "SMBus base address uninitialized, upgrade BIOS\n");
  759. return -ENODEV;
  760. }
  761. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  762. "SMBus iSMT adapter at %lx", start);
  763. dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
  764. dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
  765. err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
  766. if (err) {
  767. dev_err(&pdev->dev, "ACPI resource conflict!\n");
  768. return err;
  769. }
  770. err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
  771. if (err) {
  772. dev_err(&pdev->dev,
  773. "Failed to request SMBus region 0x%lx-0x%lx\n",
  774. start, start + len);
  775. return err;
  776. }
  777. priv->smba = pcim_iomap(pdev, SMBBAR, len);
  778. if (!priv->smba) {
  779. dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
  780. err = -ENODEV;
  781. goto fail;
  782. }
  783. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
  784. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
  785. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  786. (pci_set_consistent_dma_mask(pdev,
  787. DMA_BIT_MASK(32)) != 0)) {
  788. dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
  789. pdev);
  790. err = -ENODEV;
  791. goto fail;
  792. }
  793. }
  794. err = ismt_dev_init(priv);
  795. if (err)
  796. goto fail;
  797. ismt_hw_init(priv);
  798. err = ismt_int_init(priv);
  799. if (err)
  800. goto fail;
  801. err = i2c_add_adapter(&priv->adapter);
  802. if (err) {
  803. dev_err(&pdev->dev, "Failed to add SMBus iSMT adapter\n");
  804. err = -ENODEV;
  805. goto fail;
  806. }
  807. return 0;
  808. fail:
  809. pci_release_region(pdev, SMBBAR);
  810. return err;
  811. }
  812. /**
  813. * ismt_remove() - release driver resources
  814. * @pdev: PCI-Express device
  815. */
  816. static void ismt_remove(struct pci_dev *pdev)
  817. {
  818. struct ismt_priv *priv = pci_get_drvdata(pdev);
  819. i2c_del_adapter(&priv->adapter);
  820. pci_release_region(pdev, SMBBAR);
  821. }
  822. /**
  823. * ismt_suspend() - place the device in suspend
  824. * @pdev: PCI-Express device
  825. * @mesg: PM message
  826. */
  827. #ifdef CONFIG_PM
  828. static int ismt_suspend(struct pci_dev *pdev, pm_message_t mesg)
  829. {
  830. pci_save_state(pdev);
  831. pci_set_power_state(pdev, pci_choose_state(pdev, mesg));
  832. return 0;
  833. }
  834. /**
  835. * ismt_resume() - PCI resume code
  836. * @pdev: PCI-Express device
  837. */
  838. static int ismt_resume(struct pci_dev *pdev)
  839. {
  840. pci_set_power_state(pdev, PCI_D0);
  841. pci_restore_state(pdev);
  842. return pci_enable_device(pdev);
  843. }
  844. #else
  845. #define ismt_suspend NULL
  846. #define ismt_resume NULL
  847. #endif
  848. static struct pci_driver ismt_driver = {
  849. .name = "ismt_smbus",
  850. .id_table = ismt_ids,
  851. .probe = ismt_probe,
  852. .remove = ismt_remove,
  853. .suspend = ismt_suspend,
  854. .resume = ismt_resume,
  855. };
  856. module_pci_driver(ismt_driver);
  857. MODULE_LICENSE("Dual BSD/GPL");
  858. MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
  859. MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");