Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * intel_scu_ipc.c: Driver for the Intel SCU IPC mechanism |
| 3 | * |
| 4 | * (C) Copyright 2008-2010 Intel Corporation |
| 5 | * Author: Sreedhara DS (sreedhara.ds@intel.com) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; version 2 |
| 10 | * of the License. |
| 11 | * |
Lucas De Marchi | c844033 | 2011-03-17 17:18:22 -0300 | [diff] [blame] | 12 | * SCU running in ARC processor communicates with other entity running in IA |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 13 | * core through IPC mechanism which in turn messaging between IA core ad SCU. |
| 14 | * SCU has two IPC mechanism IPC-1 and IPC-2. IPC-1 is used between IA32 and |
| 15 | * SCU where IPC-2 is used between P-Unit and SCU. This driver delas with |
| 16 | * IPC-1 Driver provides an API for power control unit registers (e.g. MSIC) |
| 17 | * along with other APIs. |
| 18 | */ |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/init.h> |
Kay Sievers | edbaa60 | 2011-12-21 16:26:03 -0800 | [diff] [blame] | 22 | #include <linux/device.h> |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 23 | #include <linux/pm.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/interrupt.h> |
Alan Cox | 209009b | 2010-09-13 15:55:05 +0100 | [diff] [blame] | 26 | #include <linux/sfi.h> |
Paul Gortmaker | 7c52d55 | 2011-05-27 12:33:10 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Kuppuswamy Sathyanarayanan | 05454c2 | 2013-10-17 15:35:27 -0700 | [diff] [blame] | 28 | #include <asm/intel-mid.h> |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 29 | #include <asm/intel_scu_ipc.h> |
| 30 | |
| 31 | /* IPC defines the following message types */ |
| 32 | #define IPCMSG_WATCHDOG_TIMER 0xF8 /* Set Kernel Watchdog Threshold */ |
| 33 | #define IPCMSG_BATTERY 0xEF /* Coulomb Counter Accumulator */ |
| 34 | #define IPCMSG_FW_UPDATE 0xFE /* Firmware update */ |
| 35 | #define IPCMSG_PCNTRL 0xFF /* Power controller unit read/write */ |
| 36 | #define IPCMSG_FW_REVISION 0xF4 /* Get firmware revision */ |
| 37 | |
| 38 | /* Command id associated with message IPCMSG_PCNTRL */ |
| 39 | #define IPC_CMD_PCNTRL_W 0 /* Register write */ |
| 40 | #define IPC_CMD_PCNTRL_R 1 /* Register read */ |
| 41 | #define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */ |
| 42 | |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 43 | /* |
| 44 | * IPC register summary |
| 45 | * |
| 46 | * IPC register blocks are memory mapped at fixed address of 0xFF11C000 |
| 47 | * To read or write information to the SCU, driver writes to IPC-1 memory |
| 48 | * mapped registers (base address 0xFF11C000). The following is the IPC |
| 49 | * mechanism |
| 50 | * |
| 51 | * 1. IA core cDMI interface claims this transaction and converts it to a |
| 52 | * Transaction Layer Packet (TLP) message which is sent across the cDMI. |
| 53 | * |
| 54 | * 2. South Complex cDMI block receives this message and writes it to |
| 55 | * the IPC-1 register block, causing an interrupt to the SCU |
| 56 | * |
| 57 | * 3. SCU firmware decodes this interrupt and IPC message and the appropriate |
| 58 | * message handler is called within firmware. |
| 59 | */ |
| 60 | |
Arjan van de Ven | 51cd525 | 2010-07-26 10:04:24 +0100 | [diff] [blame] | 61 | #define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */ |
| 62 | #define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */ |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 63 | |
| 64 | enum { |
| 65 | SCU_IPC_LINCROFT, |
Kuppuswamy Sathyanarayanan | 7f95afb | 2013-11-14 14:15:05 -0800 | [diff] [blame] | 66 | SCU_IPC_PENWELL, |
| 67 | SCU_IPC_CLOVERVIEW, |
| 68 | SCU_IPC_TANGIER, |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /* intel scu ipc driver data*/ |
| 72 | struct intel_scu_ipc_pdata_t { |
| 73 | u32 ipc_base; |
| 74 | u32 i2c_base; |
| 75 | u32 ipc_len; |
| 76 | u32 i2c_len; |
| 77 | }; |
| 78 | |
| 79 | static struct intel_scu_ipc_pdata_t intel_scu_ipc_pdata[] = { |
| 80 | [SCU_IPC_LINCROFT] = { |
| 81 | .ipc_base = 0xff11c000, |
| 82 | .i2c_base = 0xff12b000, |
| 83 | .ipc_len = 0x100, |
| 84 | .i2c_len = 0x10, |
| 85 | }, |
Kuppuswamy Sathyanarayanan | 7f95afb | 2013-11-14 14:15:05 -0800 | [diff] [blame] | 86 | [SCU_IPC_PENWELL] = { |
| 87 | .ipc_base = 0xff11c000, |
| 88 | .i2c_base = 0xff12b000, |
| 89 | .ipc_len = 0x100, |
| 90 | .i2c_len = 0x10, |
| 91 | }, |
| 92 | [SCU_IPC_CLOVERVIEW] = { |
| 93 | .ipc_base = 0xff11c000, |
| 94 | .i2c_base = 0xff12b000, |
| 95 | .ipc_len = 0x100, |
| 96 | .i2c_len = 0x10, |
| 97 | }, |
| 98 | [SCU_IPC_TANGIER] = { |
| 99 | .ipc_base = 0xff009000, |
| 100 | .i2c_base = 0xff00d000, |
| 101 | .ipc_len = 0x100, |
| 102 | .i2c_len = 0x10, |
| 103 | }, |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 104 | }; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 105 | |
| 106 | static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id); |
| 107 | static void ipc_remove(struct pci_dev *pdev); |
| 108 | |
| 109 | struct intel_scu_ipc_dev { |
| 110 | struct pci_dev *pdev; |
| 111 | void __iomem *ipc_base; |
| 112 | void __iomem *i2c_base; |
| 113 | }; |
| 114 | |
| 115 | static struct intel_scu_ipc_dev ipcdev; /* Only one for now */ |
| 116 | |
Sreedhara DS | 14d10f0 | 2010-07-26 10:02:25 +0100 | [diff] [blame] | 117 | static int platform; /* Platform type */ |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * IPC Read Buffer (Read Only): |
| 121 | * 16 byte buffer for receiving data from SCU, if IPC command |
| 122 | * processing results in response data |
| 123 | */ |
| 124 | #define IPC_READ_BUFFER 0x90 |
| 125 | |
| 126 | #define IPC_I2C_CNTRL_ADDR 0 |
| 127 | #define I2C_DATA_ADDR 0x04 |
| 128 | |
| 129 | static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */ |
| 130 | |
| 131 | /* |
| 132 | * Command Register (Write Only): |
| 133 | * A write to this register results in an interrupt to the SCU core processor |
| 134 | * Format: |
| 135 | * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)| |
| 136 | */ |
| 137 | static inline void ipc_command(u32 cmd) /* Send ipc command */ |
| 138 | { |
| 139 | writel(cmd, ipcdev.ipc_base); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * IPC Write Buffer (Write Only): |
| 144 | * 16-byte buffer for sending data associated with IPC command to |
| 145 | * SCU. Size of the data is specified in the IPC_COMMAND_REG register |
| 146 | */ |
| 147 | static inline void ipc_data_writel(u32 data, u32 offset) /* Write ipc data */ |
| 148 | { |
| 149 | writel(data, ipcdev.ipc_base + 0x80 + offset); |
| 150 | } |
| 151 | |
| 152 | /* |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 153 | * Status Register (Read Only): |
| 154 | * Driver will read this register to get the ready/busy status of the IPC |
| 155 | * block and error status of the IPC command that was just processed by SCU |
| 156 | * Format: |
| 157 | * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)| |
| 158 | */ |
| 159 | |
| 160 | static inline u8 ipc_read_status(void) |
| 161 | { |
| 162 | return __raw_readl(ipcdev.ipc_base + 0x04); |
| 163 | } |
| 164 | |
| 165 | static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */ |
| 166 | { |
| 167 | return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset); |
| 168 | } |
| 169 | |
Sreedhara DS | e3359fd | 2010-07-26 10:02:46 +0100 | [diff] [blame] | 170 | static inline u32 ipc_data_readl(u32 offset) /* Read ipc u32 data */ |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 171 | { |
| 172 | return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset); |
| 173 | } |
| 174 | |
| 175 | static inline int busy_loop(void) /* Wait till scu status is busy */ |
| 176 | { |
| 177 | u32 status = 0; |
| 178 | u32 loop_count = 0; |
| 179 | |
| 180 | status = ipc_read_status(); |
| 181 | while (status & 1) { |
| 182 | udelay(1); /* scu processing time is in few u secods */ |
| 183 | status = ipc_read_status(); |
| 184 | loop_count++; |
| 185 | /* break if scu doesn't reset busy bit after huge retry */ |
| 186 | if (loop_count > 100000) { |
| 187 | dev_err(&ipcdev.pdev->dev, "IPC timed out"); |
| 188 | return -ETIMEDOUT; |
| 189 | } |
| 190 | } |
Hong Liu | 77e01d6 | 2010-07-26 10:06:12 +0100 | [diff] [blame] | 191 | if ((status >> 1) & 1) |
| 192 | return -EIO; |
| 193 | |
| 194 | return 0; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */ |
| 198 | static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id) |
| 199 | { |
Alan Cox | 4707375 | 2012-03-05 15:01:02 -0800 | [diff] [blame] | 200 | int nc; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 201 | u32 offset = 0; |
Axel Lin | ecb5646 | 2011-01-25 14:12:12 +0000 | [diff] [blame] | 202 | int err; |
Sreedhara DS | e3359fd | 2010-07-26 10:02:46 +0100 | [diff] [blame] | 203 | u8 cbuf[IPC_WWBUF_SIZE] = { }; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 204 | u32 *wbuf = (u32 *)&cbuf; |
| 205 | |
| 206 | mutex_lock(&ipclock); |
Sreedhara DS | e3359fd | 2010-07-26 10:02:46 +0100 | [diff] [blame] | 207 | |
Arjan van de Ven | ed6f2b4 | 2010-07-26 10:04:37 +0100 | [diff] [blame] | 208 | memset(cbuf, 0, sizeof(cbuf)); |
| 209 | |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 210 | if (ipcdev.pdev == NULL) { |
| 211 | mutex_unlock(&ipclock); |
| 212 | return -ENODEV; |
| 213 | } |
| 214 | |
Alan Cox | 4707375 | 2012-03-05 15:01:02 -0800 | [diff] [blame] | 215 | for (nc = 0; nc < count; nc++, offset += 2) { |
| 216 | cbuf[offset] = addr[nc]; |
| 217 | cbuf[offset + 1] = addr[nc] >> 8; |
| 218 | } |
Sreedhara DS | e3359fd | 2010-07-26 10:02:46 +0100 | [diff] [blame] | 219 | |
Alan Cox | 4707375 | 2012-03-05 15:01:02 -0800 | [diff] [blame] | 220 | if (id == IPC_CMD_PCNTRL_R) { |
| 221 | for (nc = 0, offset = 0; nc < count; nc++, offset += 4) |
| 222 | ipc_data_writel(wbuf[nc], offset); |
| 223 | ipc_command((count*2) << 16 | id << 12 | 0 << 8 | op); |
| 224 | } else if (id == IPC_CMD_PCNTRL_W) { |
| 225 | for (nc = 0; nc < count; nc++, offset += 1) |
| 226 | cbuf[offset] = data[nc]; |
| 227 | for (nc = 0, offset = 0; nc < count; nc++, offset += 4) |
| 228 | ipc_data_writel(wbuf[nc], offset); |
| 229 | ipc_command((count*3) << 16 | id << 12 | 0 << 8 | op); |
| 230 | } else if (id == IPC_CMD_PCNTRL_M) { |
| 231 | cbuf[offset] = data[0]; |
| 232 | cbuf[offset + 1] = data[1]; |
| 233 | ipc_data_writel(wbuf[0], 0); /* Write wbuff */ |
| 234 | ipc_command(4 << 16 | id << 12 | 0 << 8 | op); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 237 | err = busy_loop(); |
Kuppuswamy Sathyanarayanan | c7094d1 | 2013-11-14 14:15:06 -0800 | [diff] [blame^] | 238 | if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */ |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 239 | /* Workaround: values are read as 0 without memcpy_fromio */ |
Sreedhara DS | e3359fd | 2010-07-26 10:02:46 +0100 | [diff] [blame] | 240 | memcpy_fromio(cbuf, ipcdev.ipc_base + 0x90, 16); |
Alan Cox | 4707375 | 2012-03-05 15:01:02 -0800 | [diff] [blame] | 241 | for (nc = 0; nc < count; nc++) |
| 242 | data[nc] = ipc_data_readb(nc); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 243 | } |
| 244 | mutex_unlock(&ipclock); |
| 245 | return err; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * intel_scu_ipc_ioread8 - read a word via the SCU |
| 250 | * @addr: register on SCU |
| 251 | * @data: return pointer for read byte |
| 252 | * |
| 253 | * Read a single register. Returns 0 on success or an error code. All |
| 254 | * locking between SCU accesses is handled for the caller. |
| 255 | * |
| 256 | * This function may sleep. |
| 257 | */ |
| 258 | int intel_scu_ipc_ioread8(u16 addr, u8 *data) |
| 259 | { |
| 260 | return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R); |
| 261 | } |
| 262 | EXPORT_SYMBOL(intel_scu_ipc_ioread8); |
| 263 | |
| 264 | /** |
| 265 | * intel_scu_ipc_ioread16 - read a word via the SCU |
| 266 | * @addr: register on SCU |
| 267 | * @data: return pointer for read word |
| 268 | * |
| 269 | * Read a register pair. Returns 0 on success or an error code. All |
| 270 | * locking between SCU accesses is handled for the caller. |
| 271 | * |
| 272 | * This function may sleep. |
| 273 | */ |
| 274 | int intel_scu_ipc_ioread16(u16 addr, u16 *data) |
| 275 | { |
| 276 | u16 x[2] = {addr, addr + 1 }; |
| 277 | return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R); |
| 278 | } |
| 279 | EXPORT_SYMBOL(intel_scu_ipc_ioread16); |
| 280 | |
| 281 | /** |
| 282 | * intel_scu_ipc_ioread32 - read a dword via the SCU |
| 283 | * @addr: register on SCU |
| 284 | * @data: return pointer for read dword |
| 285 | * |
| 286 | * Read four registers. Returns 0 on success or an error code. All |
| 287 | * locking between SCU accesses is handled for the caller. |
| 288 | * |
| 289 | * This function may sleep. |
| 290 | */ |
| 291 | int intel_scu_ipc_ioread32(u16 addr, u32 *data) |
| 292 | { |
| 293 | u16 x[4] = {addr, addr + 1, addr + 2, addr + 3}; |
| 294 | return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R); |
| 295 | } |
| 296 | EXPORT_SYMBOL(intel_scu_ipc_ioread32); |
| 297 | |
| 298 | /** |
| 299 | * intel_scu_ipc_iowrite8 - write a byte via the SCU |
| 300 | * @addr: register on SCU |
| 301 | * @data: byte to write |
| 302 | * |
| 303 | * Write a single register. Returns 0 on success or an error code. All |
| 304 | * locking between SCU accesses is handled for the caller. |
| 305 | * |
| 306 | * This function may sleep. |
| 307 | */ |
| 308 | int intel_scu_ipc_iowrite8(u16 addr, u8 data) |
| 309 | { |
| 310 | return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W); |
| 311 | } |
| 312 | EXPORT_SYMBOL(intel_scu_ipc_iowrite8); |
| 313 | |
| 314 | /** |
| 315 | * intel_scu_ipc_iowrite16 - write a word via the SCU |
| 316 | * @addr: register on SCU |
| 317 | * @data: word to write |
| 318 | * |
| 319 | * Write two registers. Returns 0 on success or an error code. All |
| 320 | * locking between SCU accesses is handled for the caller. |
| 321 | * |
| 322 | * This function may sleep. |
| 323 | */ |
| 324 | int intel_scu_ipc_iowrite16(u16 addr, u16 data) |
| 325 | { |
| 326 | u16 x[2] = {addr, addr + 1 }; |
| 327 | return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W); |
| 328 | } |
| 329 | EXPORT_SYMBOL(intel_scu_ipc_iowrite16); |
| 330 | |
| 331 | /** |
| 332 | * intel_scu_ipc_iowrite32 - write a dword via the SCU |
| 333 | * @addr: register on SCU |
| 334 | * @data: dword to write |
| 335 | * |
| 336 | * Write four registers. Returns 0 on success or an error code. All |
| 337 | * locking between SCU accesses is handled for the caller. |
| 338 | * |
| 339 | * This function may sleep. |
| 340 | */ |
| 341 | int intel_scu_ipc_iowrite32(u16 addr, u32 data) |
| 342 | { |
| 343 | u16 x[4] = {addr, addr + 1, addr + 2, addr + 3}; |
| 344 | return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W); |
| 345 | } |
| 346 | EXPORT_SYMBOL(intel_scu_ipc_iowrite32); |
| 347 | |
| 348 | /** |
| 349 | * intel_scu_ipc_readvv - read a set of registers |
| 350 | * @addr: register list |
| 351 | * @data: bytes to return |
| 352 | * @len: length of array |
| 353 | * |
| 354 | * Read registers. Returns 0 on success or an error code. All |
| 355 | * locking between SCU accesses is handled for the caller. |
| 356 | * |
| 357 | * The largest array length permitted by the hardware is 5 items. |
| 358 | * |
| 359 | * This function may sleep. |
| 360 | */ |
| 361 | int intel_scu_ipc_readv(u16 *addr, u8 *data, int len) |
| 362 | { |
| 363 | return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R); |
| 364 | } |
| 365 | EXPORT_SYMBOL(intel_scu_ipc_readv); |
| 366 | |
| 367 | /** |
| 368 | * intel_scu_ipc_writev - write a set of registers |
| 369 | * @addr: register list |
| 370 | * @data: bytes to write |
| 371 | * @len: length of array |
| 372 | * |
| 373 | * Write registers. Returns 0 on success or an error code. All |
| 374 | * locking between SCU accesses is handled for the caller. |
| 375 | * |
| 376 | * The largest array length permitted by the hardware is 5 items. |
| 377 | * |
| 378 | * This function may sleep. |
| 379 | * |
| 380 | */ |
| 381 | int intel_scu_ipc_writev(u16 *addr, u8 *data, int len) |
| 382 | { |
| 383 | return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W); |
| 384 | } |
| 385 | EXPORT_SYMBOL(intel_scu_ipc_writev); |
| 386 | |
| 387 | |
| 388 | /** |
| 389 | * intel_scu_ipc_update_register - r/m/w a register |
| 390 | * @addr: register address |
| 391 | * @bits: bits to update |
| 392 | * @mask: mask of bits to update |
| 393 | * |
| 394 | * Read-modify-write power control unit register. The first data argument |
| 395 | * must be register value and second is mask value |
| 396 | * mask is a bitmap that indicates which bits to update. |
| 397 | * 0 = masked. Don't modify this bit, 1 = modify this bit. |
| 398 | * returns 0 on success or an error code. |
| 399 | * |
| 400 | * This function may sleep. Locking between SCU accesses is handled |
| 401 | * for the caller. |
| 402 | */ |
| 403 | int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask) |
| 404 | { |
| 405 | u8 data[2] = { bits, mask }; |
| 406 | return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M); |
| 407 | } |
| 408 | EXPORT_SYMBOL(intel_scu_ipc_update_register); |
| 409 | |
| 410 | /** |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 411 | * intel_scu_ipc_simple_command - send a simple command |
| 412 | * @cmd: command |
| 413 | * @sub: sub type |
| 414 | * |
| 415 | * Issue a simple command to the SCU. Do not use this interface if |
| 416 | * you must then access data as any data values may be overwritten |
| 417 | * by another SCU access by the time this function returns. |
| 418 | * |
| 419 | * This function may sleep. Locking for SCU accesses is handled for |
| 420 | * the caller. |
| 421 | */ |
| 422 | int intel_scu_ipc_simple_command(int cmd, int sub) |
| 423 | { |
Axel Lin | ecb5646 | 2011-01-25 14:12:12 +0000 | [diff] [blame] | 424 | int err; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 425 | |
| 426 | mutex_lock(&ipclock); |
| 427 | if (ipcdev.pdev == NULL) { |
| 428 | mutex_unlock(&ipclock); |
| 429 | return -ENODEV; |
| 430 | } |
Sreedhara DS | b4fd4f8 | 2010-07-19 09:37:42 +0100 | [diff] [blame] | 431 | ipc_command(sub << 12 | cmd); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 432 | err = busy_loop(); |
| 433 | mutex_unlock(&ipclock); |
| 434 | return err; |
| 435 | } |
| 436 | EXPORT_SYMBOL(intel_scu_ipc_simple_command); |
| 437 | |
| 438 | /** |
| 439 | * intel_scu_ipc_command - command with data |
| 440 | * @cmd: command |
| 441 | * @sub: sub type |
| 442 | * @in: input data |
Sreedhara DS | b4fd4f8 | 2010-07-19 09:37:42 +0100 | [diff] [blame] | 443 | * @inlen: input length in dwords |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 444 | * @out: output data |
Sreedhara DS | b4fd4f8 | 2010-07-19 09:37:42 +0100 | [diff] [blame] | 445 | * @outlein: output length in dwords |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 446 | * |
| 447 | * Issue a command to the SCU which involves data transfers. Do the |
| 448 | * data copies under the lock but leave it for the caller to interpret |
| 449 | */ |
| 450 | |
| 451 | int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen, |
| 452 | u32 *out, int outlen) |
| 453 | { |
Axel Lin | ecb5646 | 2011-01-25 14:12:12 +0000 | [diff] [blame] | 454 | int i, err; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 455 | |
| 456 | mutex_lock(&ipclock); |
| 457 | if (ipcdev.pdev == NULL) { |
| 458 | mutex_unlock(&ipclock); |
| 459 | return -ENODEV; |
| 460 | } |
| 461 | |
| 462 | for (i = 0; i < inlen; i++) |
| 463 | ipc_data_writel(*in++, 4 * i); |
| 464 | |
Hong Liu | 5aa0693 | 2010-07-26 10:06:31 +0100 | [diff] [blame] | 465 | ipc_command((inlen << 16) | (sub << 12) | cmd); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 466 | err = busy_loop(); |
| 467 | |
Kuppuswamy Sathyanarayanan | c7094d1 | 2013-11-14 14:15:06 -0800 | [diff] [blame^] | 468 | if (!err) { |
| 469 | for (i = 0; i < outlen; i++) |
| 470 | *out++ = ipc_data_readl(4 * i); |
| 471 | } |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 472 | |
| 473 | mutex_unlock(&ipclock); |
| 474 | return err; |
| 475 | } |
| 476 | EXPORT_SYMBOL(intel_scu_ipc_command); |
| 477 | |
| 478 | /*I2C commands */ |
| 479 | #define IPC_I2C_WRITE 1 /* I2C Write command */ |
| 480 | #define IPC_I2C_READ 2 /* I2C Read command */ |
| 481 | |
| 482 | /** |
| 483 | * intel_scu_ipc_i2c_cntrl - I2C read/write operations |
| 484 | * @addr: I2C address + command bits |
| 485 | * @data: data to read/write |
| 486 | * |
| 487 | * Perform an an I2C read/write operation via the SCU. All locking is |
| 488 | * handled for the caller. This function may sleep. |
| 489 | * |
| 490 | * Returns an error code or 0 on success. |
| 491 | * |
| 492 | * This has to be in the IPC driver for the locking. |
| 493 | */ |
| 494 | int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data) |
| 495 | { |
| 496 | u32 cmd = 0; |
| 497 | |
| 498 | mutex_lock(&ipclock); |
Sreedhara DS | b4fd4f8 | 2010-07-19 09:37:42 +0100 | [diff] [blame] | 499 | if (ipcdev.pdev == NULL) { |
| 500 | mutex_unlock(&ipclock); |
| 501 | return -ENODEV; |
| 502 | } |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 503 | cmd = (addr >> 24) & 0xFF; |
| 504 | if (cmd == IPC_I2C_READ) { |
| 505 | writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR); |
| 506 | /* Write not getting updated without delay */ |
| 507 | mdelay(1); |
| 508 | *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR); |
| 509 | } else if (cmd == IPC_I2C_WRITE) { |
Jianwei Yang | 32e2f63 | 2010-08-24 14:32:38 +0100 | [diff] [blame] | 510 | writel(*data, ipcdev.i2c_base + I2C_DATA_ADDR); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 511 | mdelay(1); |
| 512 | writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR); |
| 513 | } else { |
| 514 | dev_err(&ipcdev.pdev->dev, |
| 515 | "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd); |
| 516 | |
| 517 | mutex_unlock(&ipclock); |
Sreedhara DS | 5369c02d | 2010-10-22 15:43:55 +0100 | [diff] [blame] | 518 | return -EIO; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 519 | } |
| 520 | mutex_unlock(&ipclock); |
| 521 | return 0; |
| 522 | } |
| 523 | EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl); |
| 524 | |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 525 | /* |
| 526 | * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1 |
| 527 | * When ioc bit is set to 1, caller api must wait for interrupt handler called |
| 528 | * which in turn unlocks the caller api. Currently this is not used |
| 529 | * |
| 530 | * This is edge triggered so we need take no action to clear anything |
| 531 | */ |
| 532 | static irqreturn_t ioc(int irq, void *dev_id) |
| 533 | { |
| 534 | return IRQ_HANDLED; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * ipc_probe - probe an Intel SCU IPC |
| 539 | * @dev: the PCI device matching |
| 540 | * @id: entry in the match table |
| 541 | * |
| 542 | * Enable and install an intel SCU IPC. This appears in the PCI space |
| 543 | * but uses some hard coded addresses as well. |
| 544 | */ |
| 545 | static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id) |
| 546 | { |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 547 | int err, pid; |
| 548 | struct intel_scu_ipc_pdata_t *pdata; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 549 | resource_size_t pci_resource; |
| 550 | |
| 551 | if (ipcdev.pdev) /* We support only one SCU */ |
| 552 | return -EBUSY; |
| 553 | |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 554 | pid = id->driver_data; |
| 555 | pdata = &intel_scu_ipc_pdata[pid]; |
| 556 | |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 557 | ipcdev.pdev = pci_dev_get(dev); |
| 558 | |
| 559 | err = pci_enable_device(dev); |
| 560 | if (err) |
| 561 | return err; |
| 562 | |
| 563 | err = pci_request_regions(dev, "intel_scu_ipc"); |
| 564 | if (err) |
| 565 | return err; |
| 566 | |
| 567 | pci_resource = pci_resource_start(dev, 0); |
| 568 | if (!pci_resource) |
| 569 | return -ENOMEM; |
| 570 | |
| 571 | if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev)) |
| 572 | return -EBUSY; |
| 573 | |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 574 | ipcdev.ipc_base = ioremap_nocache(pdata->ipc_base, pdata->ipc_len); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 575 | if (!ipcdev.ipc_base) |
| 576 | return -ENOMEM; |
| 577 | |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 578 | ipcdev.i2c_base = ioremap_nocache(pdata->i2c_base, pdata->i2c_len); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 579 | if (!ipcdev.i2c_base) { |
| 580 | iounmap(ipcdev.ipc_base); |
| 581 | return -ENOMEM; |
| 582 | } |
Feng Tang | 1da4b1c | 2010-11-09 11:22:58 +0000 | [diff] [blame] | 583 | |
| 584 | intel_scu_devices_create(); |
| 585 | |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * ipc_remove - remove a bound IPC device |
| 591 | * @pdev: PCI device |
| 592 | * |
| 593 | * In practice the SCU is not removable but this function is also |
| 594 | * called for each device on a module unload or cleanup which is the |
| 595 | * path that will get used. |
| 596 | * |
| 597 | * Free up the mappings and release the PCI resources |
| 598 | */ |
| 599 | static void ipc_remove(struct pci_dev *pdev) |
| 600 | { |
| 601 | free_irq(pdev->irq, &ipcdev); |
| 602 | pci_release_regions(pdev); |
| 603 | pci_dev_put(ipcdev.pdev); |
| 604 | iounmap(ipcdev.ipc_base); |
| 605 | iounmap(ipcdev.i2c_base); |
| 606 | ipcdev.pdev = NULL; |
Feng Tang | 1da4b1c | 2010-11-09 11:22:58 +0000 | [diff] [blame] | 607 | intel_scu_devices_destroy(); |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Axel Lin | daa7769 | 2011-07-07 10:22:46 +0800 | [diff] [blame] | 610 | static DEFINE_PCI_DEVICE_TABLE(pci_ids) = { |
Kuppuswamy Sathyanarayanan | e97a1c9 | 2013-11-14 14:15:04 -0800 | [diff] [blame] | 611 | {PCI_VDEVICE(INTEL, 0x082a), SCU_IPC_LINCROFT}, |
Kuppuswamy Sathyanarayanan | 7f95afb | 2013-11-14 14:15:05 -0800 | [diff] [blame] | 612 | {PCI_VDEVICE(INTEL, 0x080e), SCU_IPC_PENWELL}, |
| 613 | {PCI_VDEVICE(INTEL, 0x08ea), SCU_IPC_CLOVERVIEW}, |
| 614 | {PCI_VDEVICE(INTEL, 0x11a0), SCU_IPC_TANGIER}, |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 615 | { 0,} |
| 616 | }; |
| 617 | MODULE_DEVICE_TABLE(pci, pci_ids); |
| 618 | |
| 619 | static struct pci_driver ipc_driver = { |
| 620 | .name = "intel_scu_ipc", |
| 621 | .id_table = pci_ids, |
| 622 | .probe = ipc_probe, |
| 623 | .remove = ipc_remove, |
| 624 | }; |
| 625 | |
| 626 | |
| 627 | static int __init intel_scu_ipc_init(void) |
| 628 | { |
Kuppuswamy Sathyanarayanan | 712b6aa | 2013-10-17 15:35:29 -0700 | [diff] [blame] | 629 | platform = intel_mid_identify_cpu(); |
Alan Cox | 9dd3ade | 2010-07-26 10:03:58 +0100 | [diff] [blame] | 630 | if (platform == 0) |
| 631 | return -ENODEV; |
Sreedhara DS | 9a58a33 | 2010-04-26 18:13:05 +0100 | [diff] [blame] | 632 | return pci_register_driver(&ipc_driver); |
| 633 | } |
| 634 | |
| 635 | static void __exit intel_scu_ipc_exit(void) |
| 636 | { |
| 637 | pci_unregister_driver(&ipc_driver); |
| 638 | } |
| 639 | |
| 640 | MODULE_AUTHOR("Sreedhara DS <sreedhara.ds@intel.com>"); |
| 641 | MODULE_DESCRIPTION("Intel SCU IPC driver"); |
| 642 | MODULE_LICENSE("GPL"); |
| 643 | |
| 644 | module_init(intel_scu_ipc_init); |
| 645 | module_exit(intel_scu_ipc_exit); |