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