Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved. |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 13 | #include <linux/module.h> /* Just for modules */ |
| 14 | #include <linux/kernel.h> /* Only for KERN_INFO */ |
| 15 | #include <linux/err.h> /* Error macros */ |
| 16 | #include <linux/list.h> /* Linked list */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 17 | #include <linux/cdev.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 18 | #include <linux/init.h> /* Needed for the macros */ |
| 19 | #include <linux/io.h> /* IO macros */ |
| 20 | #include <linux/device.h> /* Device drivers need this */ |
| 21 | #include <linux/sched.h> /* Externally defined globals */ |
| 22 | #include <linux/pm_runtime.h> /* Runtime power management */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 23 | #include <linux/fs.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 24 | #include <linux/uaccess.h> /* copy_to_user */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 25 | #include <linux/slab.h> /* kfree, kzalloc */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 26 | #include <linux/ioport.h> /* XXX_ mem_region */ |
| 27 | #include <linux/dma-mapping.h> /* dma_XXX */ |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 28 | #include <linux/dmapool.h> /* DMA pools */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 29 | #include <linux/delay.h> /* msleep */ |
| 30 | #include <linux/platform_device.h> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 31 | #include <linux/clk.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 32 | #include <linux/poll.h> /* poll() file op */ |
| 33 | #include <linux/wait.h> /* wait() macros, sleeping */ |
| 34 | #include <linux/tspp.h> /* tspp functions */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 35 | #include <linux/bitops.h> /* BIT() macro */ |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 36 | #include <linux/regulator/consumer.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 37 | #include <mach/sps.h> /* BAM stuff */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 38 | #include <mach/gpio.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 39 | #include <linux/wakelock.h> /* Locking functions */ |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 40 | #include <linux/timer.h> /* Timer services */ |
| 41 | #include <linux/jiffies.h> /* Jiffies counter */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 42 | #include <mach/dma.h> |
| 43 | #include <mach/msm_tspp.h> |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 44 | #include <mach/rpm-regulator-smd.h> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 45 | #include <linux/debugfs.h> |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 46 | #include <linux/of.h> |
| 47 | #include <linux/of_gpio.h> |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 48 | #include <linux/string.h> |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 49 | #include <mach/msm_bus.h> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * General defines |
| 53 | */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 54 | #define TSPP_TSIF_INSTANCES 2 |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 55 | #define TSPP_GPIOS_PER_TSIF 4 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 56 | #define TSPP_FILTER_TABLES 3 |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 57 | #define TSPP_MAX_DEVICES 1 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 58 | #define TSPP_NUM_CHANNELS 16 |
| 59 | #define TSPP_NUM_PRIORITIES 16 |
| 60 | #define TSPP_NUM_KEYS 8 |
| 61 | #define INVALID_CHANNEL 0xFFFFFFFF |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * BAM descriptor FIFO size (in number of descriptors). |
| 65 | * Max number of descriptors allowed by SPS which is 8K-1. |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 66 | */ |
Hamad Kadmany | 2402035 | 2013-05-22 12:54:18 +0300 | [diff] [blame] | 67 | #define TSPP_SPS_DESCRIPTOR_COUNT (8 * 1024 - 1) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 68 | #define TSPP_PACKET_LENGTH 188 |
| 69 | #define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH) |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 70 | |
| 71 | /* Max descriptor buffer size allowed by SPS */ |
| 72 | #define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1) |
| 73 | |
| 74 | /* |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 75 | * Returns whether to use DMA pool for TSPP output buffers. |
| 76 | * For buffers smaller than page size, using DMA pool |
| 77 | * provides better memory utilization as dma_alloc_coherent |
| 78 | * allocates minimum of page size. |
| 79 | */ |
| 80 | #define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE) |
| 81 | |
| 82 | /* |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 83 | * Max allowed TSPP buffers/descriptors. |
| 84 | * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors. |
| 85 | */ |
| 86 | #define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 87 | #define TSPP_TSIF_DEFAULT_TIME_LIMIT 60 |
| 88 | #define SPS_DESCRIPTOR_SIZE 8 |
| 89 | #define MIN_ACCEPTABLE_BUFFER_COUNT 2 |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 90 | #define TSPP_DEBUG(msg...) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * TSIF register offsets |
| 94 | */ |
| 95 | #define TSIF_STS_CTL_OFF (0x0) |
| 96 | #define TSIF_TIME_LIMIT_OFF (0x4) |
| 97 | #define TSIF_CLK_REF_OFF (0x8) |
| 98 | #define TSIF_LPBK_FLAGS_OFF (0xc) |
| 99 | #define TSIF_LPBK_DATA_OFF (0x10) |
| 100 | #define TSIF_TEST_CTL_OFF (0x14) |
| 101 | #define TSIF_TEST_MODE_OFF (0x18) |
| 102 | #define TSIF_TEST_RESET_OFF (0x1c) |
| 103 | #define TSIF_TEST_EXPORT_OFF (0x20) |
| 104 | #define TSIF_TEST_CURRENT_OFF (0x24) |
| 105 | |
| 106 | #define TSIF_DATA_PORT_OFF (0x100) |
| 107 | |
| 108 | /* bits for TSIF_STS_CTL register */ |
| 109 | #define TSIF_STS_CTL_EN_IRQ BIT(28) |
| 110 | #define TSIF_STS_CTL_PACK_AVAIL BIT(27) |
| 111 | #define TSIF_STS_CTL_1ST_PACKET BIT(26) |
| 112 | #define TSIF_STS_CTL_OVERFLOW BIT(25) |
| 113 | #define TSIF_STS_CTL_LOST_SYNC BIT(24) |
| 114 | #define TSIF_STS_CTL_TIMEOUT BIT(23) |
| 115 | #define TSIF_STS_CTL_INV_SYNC BIT(21) |
| 116 | #define TSIF_STS_CTL_INV_NULL BIT(20) |
| 117 | #define TSIF_STS_CTL_INV_ERROR BIT(19) |
| 118 | #define TSIF_STS_CTL_INV_ENABLE BIT(18) |
| 119 | #define TSIF_STS_CTL_INV_DATA BIT(17) |
| 120 | #define TSIF_STS_CTL_INV_CLOCK BIT(16) |
| 121 | #define TSIF_STS_CTL_SPARE BIT(15) |
| 122 | #define TSIF_STS_CTL_EN_NULL BIT(11) |
| 123 | #define TSIF_STS_CTL_EN_ERROR BIT(10) |
| 124 | #define TSIF_STS_CTL_LAST_BIT BIT(9) |
| 125 | #define TSIF_STS_CTL_EN_TIME_LIM BIT(8) |
| 126 | #define TSIF_STS_CTL_EN_TCR BIT(7) |
| 127 | #define TSIF_STS_CTL_TEST_MODE BIT(6) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 128 | #define TSIF_STS_CTL_MODE_2 BIT(5) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 129 | #define TSIF_STS_CTL_EN_DM BIT(4) |
| 130 | #define TSIF_STS_CTL_STOP BIT(3) |
| 131 | #define TSIF_STS_CTL_START BIT(0) |
| 132 | |
| 133 | /* |
| 134 | * TSPP register offsets |
| 135 | */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 136 | #define TSPP_RST 0x00 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 137 | #define TSPP_CLK_CONTROL 0x04 |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 138 | #define TSPP_CONFIG 0x08 |
| 139 | #define TSPP_CONTROL 0x0C |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 140 | #define TSPP_PS_DISABLE 0x10 |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 141 | #define TSPP_MSG_IRQ_STATUS 0x14 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 142 | #define TSPP_MSG_IRQ_MASK 0x18 |
| 143 | #define TSPP_IRQ_STATUS 0x1C |
| 144 | #define TSPP_IRQ_MASK 0x20 |
| 145 | #define TSPP_IRQ_CLEAR 0x24 |
| 146 | #define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2)) |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 147 | #define TSPP_STATUS 0x68 |
| 148 | #define TSPP_CURR_TSP_HEADER 0x6C |
| 149 | #define TSPP_CURR_PID_FILTER 0x70 |
| 150 | #define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2)) |
| 151 | #define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2)) |
| 152 | #define TSPP_DATA_KEY_RESET 0x9C |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 153 | #define TSPP_KEY_VALID 0xA0 |
| 154 | #define TSPP_KEY_ERROR 0xA4 |
| 155 | #define TSPP_TEST_CTRL 0xA8 |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 156 | #define TSPP_VERSION 0xAC |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 157 | #define TSPP_GENERICS 0xB0 |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 158 | #define TSPP_NOP 0xB4 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Register bit definitions |
| 162 | */ |
| 163 | /* TSPP_RST */ |
| 164 | #define TSPP_RST_RESET BIT(0) |
| 165 | |
| 166 | /* TSPP_CLK_CONTROL */ |
| 167 | #define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9) |
| 168 | #define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8) |
| 169 | #define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7) |
| 170 | #define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6) |
| 171 | #define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5) |
| 172 | #define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4) |
| 173 | #define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3) |
| 174 | #define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2) |
| 175 | #define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1) |
| 176 | #define TSPP_CLK_CONTROL_SET_CLKON BIT(0) |
| 177 | |
| 178 | /* TSPP_CONFIG */ |
| 179 | #define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \ |
| 180 | ((_b & 0xF) << 8)) |
| 181 | #define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF) |
| 182 | #define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7) |
| 183 | #define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6) |
| 184 | #define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5) |
| 185 | #define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4) |
| 186 | #define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3) |
| 187 | #define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2) |
| 188 | #define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1) |
| 189 | #define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0) |
| 190 | |
| 191 | /* TSPP_CONTROL */ |
| 192 | #define TSPP_CONTROL_PID_FILTER_LOCK BIT(5) |
| 193 | #define TSPP_CONTROL_FORCE_KEY_CALC BIT(4) |
| 194 | #define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3) |
| 195 | #define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2) |
| 196 | #define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1) |
| 197 | #define TSPP_CONTROL_PERF_COUNT_INIT BIT(0) |
| 198 | |
| 199 | /* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */ |
| 200 | #define TSPP_MSG_TSPP_IRQ BIT(2) |
| 201 | #define TSPP_MSG_TSIF_1_IRQ BIT(1) |
| 202 | #define TSPP_MSG_TSIF_0_IRQ BIT(0) |
| 203 | |
| 204 | /* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 205 | #define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19) |
| 206 | #define TSPP_IRQ_STATUS_KEY_ERROR BIT(18) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 207 | #define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17) |
| 208 | #define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16) |
| 209 | #define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n)) |
| 210 | |
| 211 | /* TSPP_PIPE_ERROR_STATUS */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 212 | #define TSPP_PIPE_PES_SYNC_ERROR BIT(3) |
| 213 | #define TSPP_PIPE_PS_LENGTH_ERROR BIT(2) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 214 | #define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1) |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 215 | #define TSPP_PIP_PS_LOST_START BIT(0) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 216 | |
| 217 | /* TSPP_STATUS */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 218 | #define TSPP_STATUS_TSP_PKT_AVAIL BIT(10) |
| 219 | #define TSPP_STATUS_TSIF1_DM_REQ BIT(6) |
| 220 | #define TSPP_STATUS_TSIF0_DM_REQ BIT(2) |
| 221 | #define TSPP_CURR_FILTER_TABLE BIT(0) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 222 | |
| 223 | /* TSPP_GENERICS */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 224 | #define TSPP_GENERICS_CRYPTO_GEN BIT(12) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 225 | #define TSPP_GENERICS_MAX_CONS_PIPES BIT(7) |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 226 | #define TSPP_GENERICS_MAX_PIPES BIT(2) |
| 227 | #define TSPP_GENERICS_TSIF_1_GEN BIT(1) |
| 228 | #define TSPP_GENERICS_TSIF_0_GEN BIT(0) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * TSPP memory regions |
| 232 | */ |
| 233 | #define TSPP_PID_FILTER_TABLE0 0x800 |
| 234 | #define TSPP_PID_FILTER_TABLE1 0x880 |
| 235 | #define TSPP_PID_FILTER_TABLE2 0x900 |
| 236 | #define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */ |
| 237 | #define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */ |
| 238 | #define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */ |
| 239 | #define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2)) |
| 240 | #define TSPP_DATA_KEY 0xCD0 |
| 241 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 242 | struct debugfs_entry { |
| 243 | const char *name; |
| 244 | mode_t mode; |
| 245 | int offset; |
| 246 | }; |
| 247 | |
| 248 | static const struct debugfs_entry debugfs_tsif_regs[] = { |
| 249 | {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF}, |
| 250 | {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF}, |
| 251 | {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF}, |
| 252 | {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF}, |
| 253 | {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF}, |
| 254 | {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF}, |
| 255 | {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF}, |
| 256 | {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF}, |
| 257 | {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF}, |
| 258 | {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF}, |
| 259 | {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF}, |
| 260 | }; |
| 261 | |
| 262 | static const struct debugfs_entry debugfs_tspp_regs[] = { |
| 263 | {"rst", S_IRUGO | S_IWUSR, TSPP_RST}, |
| 264 | {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL}, |
| 265 | {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG}, |
| 266 | {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL}, |
| 267 | {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE}, |
| 268 | {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS}, |
| 269 | {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK}, |
| 270 | {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS}, |
| 271 | {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK}, |
| 272 | {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR}, |
| 273 | /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */ |
| 274 | {"status", S_IRUGO | S_IWUSR, TSPP_STATUS}, |
| 275 | {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER}, |
| 276 | {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER}, |
| 277 | /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */ |
| 278 | /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */ |
| 279 | {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET}, |
| 280 | {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID}, |
| 281 | {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR}, |
| 282 | {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL}, |
| 283 | {"version", S_IRUGO | S_IWUSR, TSPP_VERSION}, |
| 284 | {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS}, |
| 285 | {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0}, |
| 286 | {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1}, |
| 287 | {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2}, |
| 288 | {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE}, |
| 289 | {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT}, |
| 290 | {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE}, |
| 291 | {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY} |
| 292 | }; |
| 293 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 294 | struct tspp_pid_filter { |
| 295 | u32 filter; /* see FILTER_ macros */ |
| 296 | u32 config; /* see FILTER_ macros */ |
| 297 | }; |
| 298 | |
| 299 | /* tsp_info */ |
| 300 | #define FILTER_HEADER_ERROR_MASK BIT(7) |
| 301 | #define FILTER_TRANS_END_DISABLE BIT(6) |
| 302 | #define FILTER_DEC_ON_ERROR_EN BIT(5) |
| 303 | #define FILTER_DECRYPT BIT(4) |
| 304 | #define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT) |
| 305 | #define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF) |
| 306 | #define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \ |
| 307 | (_p->config & ~0xF) | (_b & 0xF)) |
| 308 | #define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3) |
| 309 | #define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \ |
| 310 | (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30)) |
| 311 | #define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF) |
| 312 | #define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \ |
| 313 | (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13)) |
| 314 | #define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF) |
| 315 | #define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \ |
| 316 | (_p->filter & ~0x1FFF) | (_b & 0x1FFF)) |
| 317 | #define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3) |
| 318 | #define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \ |
| 319 | (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30)) |
| 320 | #define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7) |
| 321 | #define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \ |
| 322 | (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8)) |
| 323 | |
| 324 | struct tspp_global_performance_regs { |
| 325 | u32 tsp_total; |
| 326 | u32 tsp_ignored; |
| 327 | u32 tsp_error; |
| 328 | u32 tsp_sync; |
| 329 | }; |
| 330 | |
| 331 | struct tspp_pipe_context_regs { |
| 332 | u16 pes_bytes_left; |
| 333 | u16 count; |
| 334 | u32 tsif_suffix; |
| 335 | } __packed; |
| 336 | #define CONTEXT_GET_STATE(_a) (_a & 0x3) |
| 337 | #define CONTEXT_UNSPEC_LENGTH BIT(11) |
| 338 | #define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF) |
| 339 | |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 340 | #define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000) |
| 341 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 342 | struct tspp_pipe_performance_regs { |
| 343 | u32 tsp_total; |
| 344 | u32 ps_duplicate_tsp; |
| 345 | u32 tsp_no_payload; |
| 346 | u32 tsp_broken_ps; |
| 347 | u32 ps_total_num; |
| 348 | u32 ps_continuity_error; |
| 349 | u32 ps_length_error; |
| 350 | u32 pes_sync_error; |
| 351 | }; |
| 352 | |
| 353 | struct tspp_tsif_device { |
| 354 | void __iomem *base; |
| 355 | u32 time_limit; |
| 356 | u32 ref_count; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 357 | enum tspp_tsif_mode mode; |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 358 | int clock_inverse; |
| 359 | int data_inverse; |
| 360 | int sync_inverse; |
| 361 | int enable_inverse; |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 362 | u32 tsif_irq; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 363 | |
| 364 | /* debugfs */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 365 | struct dentry *dent_tsif; |
| 366 | struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)]; |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 367 | u32 stat_rx; |
| 368 | u32 stat_overflow; |
| 369 | u32 stat_lost_sync; |
| 370 | u32 stat_timeout; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | enum tspp_buf_state { |
| 374 | TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */ |
| 375 | TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 376 | TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */ |
| 377 | TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | struct tspp_mem_buffer { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 381 | struct tspp_mem_buffer *next; |
| 382 | struct sps_mem_buffer sps; |
| 383 | struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 384 | enum tspp_buf_state state; |
| 385 | size_t filled; /* how much data this buffer is holding */ |
| 386 | int read_index; /* where to start reading data from */ |
| 387 | }; |
| 388 | |
| 389 | /* this represents each char device 'channel' */ |
| 390 | struct tspp_channel { |
| 391 | struct cdev cdev; |
| 392 | struct device *dd; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 393 | struct tspp_device *pdev; /* can use container_of instead? */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 394 | struct sps_pipe *pipe; |
| 395 | struct sps_connect config; |
| 396 | struct sps_register_event event; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 397 | struct tspp_mem_buffer *data; /* list of buffers */ |
| 398 | struct tspp_mem_buffer *read; /* first buffer ready to be read */ |
| 399 | struct tspp_mem_buffer *waiting; /* first outstanding transfer */ |
| 400 | struct tspp_mem_buffer *locked; /* buffer currently being read */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 401 | wait_queue_head_t in_queue; /* set when data is received */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 402 | u32 id; /* channel id (0-15) */ |
| 403 | int used; /* is this channel in use? */ |
| 404 | int key; /* which encryption key index is used */ |
| 405 | u32 buffer_size; /* size of the sps transfer buffers */ |
| 406 | u32 max_buffers; /* how many buffers should be allocated */ |
| 407 | u32 buffer_count; /* how many buffers are actually allocated */ |
| 408 | u32 filter_count; /* how many filters have been added to this channel */ |
| 409 | u32 int_freq; /* generate interrupts every x descriptors */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 410 | enum tspp_source src; |
| 411 | enum tspp_mode mode; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 412 | tspp_notifier *notifier; /* used only with kernel api */ |
| 413 | void *notify_data; /* data to be passed with the notifier */ |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 414 | u32 expiration_period_ms; /* notification on partially filled buffers */ |
| 415 | struct timer_list expiration_timer; |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 416 | struct dma_pool *dma_pool; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 417 | tspp_memfree *memfree; /* user defined memory free function */ |
| 418 | void *user_info; /* user cookie passed to memory alloc/free function */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | struct tspp_pid_filter_table { |
| 422 | struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES]; |
| 423 | }; |
| 424 | |
| 425 | struct tspp_key_entry { |
| 426 | u32 even_lsb; |
| 427 | u32 even_msb; |
| 428 | u32 odd_lsb; |
| 429 | u32 odd_msb; |
| 430 | }; |
| 431 | |
| 432 | struct tspp_key_table { |
| 433 | struct tspp_key_entry entry[TSPP_NUM_KEYS]; |
| 434 | }; |
| 435 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 436 | /* this represents the actual hardware device */ |
| 437 | struct tspp_device { |
| 438 | struct list_head devlist; /* list of all devices */ |
| 439 | struct platform_device *pdev; |
| 440 | void __iomem *base; |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 441 | uint32_t tsif_bus_client; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 442 | unsigned int tspp_irq; |
| 443 | unsigned int bam_irq; |
| 444 | u32 bam_handle; |
| 445 | struct sps_bam_props bam_props; |
| 446 | struct wake_lock wake_lock; |
| 447 | spinlock_t spinlock; |
| 448 | struct tasklet_struct tlet; |
| 449 | struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES]; |
| 450 | /* clocks */ |
| 451 | struct clk *tsif_pclk; |
| 452 | struct clk *tsif_ref_clk; |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 453 | /* regulators */ |
| 454 | struct regulator *tsif_vreg; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 455 | /* data */ |
| 456 | struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES]; |
| 457 | struct tspp_channel channels[TSPP_NUM_CHANNELS]; |
| 458 | struct tspp_key_table *tspp_key_table; |
| 459 | struct tspp_global_performance_regs *tspp_global_performance; |
| 460 | struct tspp_pipe_context_regs *tspp_pipe_context; |
| 461 | struct tspp_pipe_performance_regs *tspp_pipe_performance; |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 462 | bool req_irqs; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 463 | |
| 464 | struct dentry *dent; |
| 465 | struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)]; |
| 466 | }; |
| 467 | |
| 468 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 469 | static struct class *tspp_class; |
| 470 | static int tspp_key_entry; |
| 471 | static dev_t tspp_minor; /* next minor number to assign */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 472 | |
| 473 | static LIST_HEAD(tspp_devices); |
| 474 | |
| 475 | /* forward declarations */ |
| 476 | static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *); |
| 477 | static ssize_t tspp_open(struct inode *inode, struct file *filp); |
| 478 | static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p); |
| 479 | static ssize_t tspp_release(struct inode *inode, struct file *filp); |
| 480 | static long tspp_ioctl(struct file *, unsigned int, unsigned long); |
| 481 | |
| 482 | /* file operations */ |
| 483 | static const struct file_operations tspp_fops = { |
| 484 | .owner = THIS_MODULE, |
| 485 | .read = tspp_read, |
| 486 | .open = tspp_open, |
| 487 | .poll = tspp_poll, |
| 488 | .release = tspp_release, |
| 489 | .unlocked_ioctl = tspp_ioctl, |
| 490 | }; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 491 | |
| 492 | /*** IRQ ***/ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 493 | static irqreturn_t tspp_isr(int irq, void *dev) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 494 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 495 | struct tspp_device *device = dev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 496 | u32 status, mask; |
| 497 | u32 data; |
| 498 | |
| 499 | status = readl_relaxed(device->base + TSPP_IRQ_STATUS); |
| 500 | mask = readl_relaxed(device->base + TSPP_IRQ_MASK); |
| 501 | status &= mask; |
| 502 | |
| 503 | if (!status) { |
| 504 | dev_warn(&device->pdev->dev, "Spurious interrupt"); |
| 505 | return IRQ_NONE; |
| 506 | } |
| 507 | |
| 508 | /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */ |
| 509 | |
| 510 | if (status & TSPP_IRQ_STATUS_KEY_ERROR) { |
| 511 | /* read the key error info */ |
| 512 | data = readl_relaxed(device->base + TSPP_KEY_ERROR); |
| 513 | dev_info(&device->pdev->dev, "key error 0x%x", data); |
| 514 | } |
| 515 | if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) { |
| 516 | data = readl_relaxed(device->base + TSPP_KEY_VALID); |
| 517 | dev_info(&device->pdev->dev, "key invalidated: 0x%x", data); |
| 518 | } |
| 519 | if (status & TSPP_IRQ_STATUS_KEY_SWITCHED) |
| 520 | dev_info(&device->pdev->dev, "key switched"); |
| 521 | |
| 522 | if (status & 0xffff) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 523 | dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 524 | |
| 525 | writel_relaxed(status, device->base + TSPP_IRQ_CLEAR); |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Before returning IRQ_HANDLED to the generic interrupt handling |
| 529 | * framework need to make sure all operations including clearing of |
| 530 | * interrupt status registers in the hardware is performed. |
| 531 | * Thus a barrier after clearing the interrupt status register |
| 532 | * is required to guarantee that the interrupt status register has |
| 533 | * really been cleared by the time we return from this handler. |
| 534 | */ |
| 535 | wmb(); |
| 536 | return IRQ_HANDLED; |
| 537 | } |
| 538 | |
| 539 | static irqreturn_t tsif_isr(int irq, void *dev) |
| 540 | { |
| 541 | struct tspp_tsif_device *tsif_device = dev; |
| 542 | u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF); |
| 543 | |
| 544 | if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL | |
| 545 | TSIF_STS_CTL_OVERFLOW | |
| 546 | TSIF_STS_CTL_LOST_SYNC | |
| 547 | TSIF_STS_CTL_TIMEOUT))) |
| 548 | return IRQ_NONE; |
| 549 | |
| 550 | if (sts_ctl & TSIF_STS_CTL_OVERFLOW) |
| 551 | tsif_device->stat_overflow++; |
| 552 | |
| 553 | if (sts_ctl & TSIF_STS_CTL_LOST_SYNC) |
| 554 | tsif_device->stat_lost_sync++; |
| 555 | |
| 556 | if (sts_ctl & TSIF_STS_CTL_TIMEOUT) |
| 557 | tsif_device->stat_timeout++; |
| 558 | |
| 559 | iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF); |
| 560 | |
| 561 | /* |
| 562 | * Before returning IRQ_HANDLED to the generic interrupt handling |
| 563 | * framework need to make sure all operations including clearing of |
| 564 | * interrupt status registers in the hardware is performed. |
| 565 | * Thus a barrier after clearing the interrupt status register |
| 566 | * is required to guarantee that the interrupt status register has |
| 567 | * really been cleared by the time we return from this handler. |
| 568 | */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 569 | wmb(); |
| 570 | return IRQ_HANDLED; |
| 571 | } |
| 572 | |
| 573 | /*** callbacks ***/ |
| 574 | static void tspp_sps_complete_cb(struct sps_event_notify *notify) |
| 575 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 576 | struct tspp_device *pdev = notify->user; |
| 577 | tasklet_schedule(&pdev->tlet); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 580 | static void tspp_expiration_timer(unsigned long data) |
| 581 | { |
| 582 | struct tspp_device *pdev = (struct tspp_device *)data; |
| 583 | |
| 584 | if (pdev) |
| 585 | tasklet_schedule(&pdev->tlet); |
| 586 | } |
| 587 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 588 | /*** tasklet ***/ |
| 589 | static void tspp_sps_complete_tlet(unsigned long data) |
| 590 | { |
| 591 | int i; |
| 592 | int complete; |
| 593 | unsigned long flags; |
| 594 | struct sps_iovec iovec; |
| 595 | struct tspp_channel *channel; |
| 596 | struct tspp_device *device = (struct tspp_device *)data; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 597 | spin_lock_irqsave(&device->spinlock, flags); |
| 598 | |
| 599 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) { |
| 600 | complete = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 601 | channel = &device->channels[i]; |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 602 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 603 | if (!channel->used || !channel->waiting) |
| 604 | continue; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 605 | |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 606 | /* stop the expiration timer */ |
| 607 | if (channel->expiration_period_ms) |
| 608 | del_timer(&channel->expiration_timer); |
| 609 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 610 | /* get completions */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 611 | while (channel->waiting->state == TSPP_BUF_STATE_WAITING) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 612 | if (sps_get_iovec(channel->pipe, &iovec) != 0) { |
| 613 | pr_err("tspp: Error in iovec on channel %i", |
| 614 | channel->id); |
| 615 | break; |
| 616 | } |
| 617 | if (iovec.size == 0) |
| 618 | break; |
| 619 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 620 | if (iovec.addr != channel->waiting->sps.phys_base) |
Hamad Kadmany | a23457e | 2013-07-02 16:51:20 +0300 | [diff] [blame] | 621 | pr_err("tspp: buffer mismatch %pa", |
| 622 | &channel->waiting->sps.phys_base); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 623 | |
| 624 | complete = 1; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 625 | channel->waiting->state = TSPP_BUF_STATE_DATA; |
| 626 | channel->waiting->filled = iovec.size; |
| 627 | channel->waiting->read_index = 0; |
| 628 | |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 629 | if (channel->src == TSPP_SOURCE_TSIF0) |
| 630 | device->tsif[0].stat_rx++; |
| 631 | else if (channel->src == TSPP_SOURCE_TSIF1) |
| 632 | device->tsif[1].stat_rx++; |
| 633 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 634 | /* update the pointers */ |
| 635 | channel->waiting = channel->waiting->next; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 638 | /* wake any waiting processes */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 639 | if (complete) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 640 | wake_up_interruptible(&channel->in_queue); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 641 | |
| 642 | /* call notifiers */ |
| 643 | if (channel->notifier) |
| 644 | channel->notifier(channel->id, |
| 645 | channel->notify_data); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 646 | } |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 647 | |
| 648 | /* restart expiration timer */ |
| 649 | if (channel->expiration_period_ms) |
| 650 | mod_timer(&channel->expiration_timer, |
| 651 | jiffies + |
| 652 | MSEC_TO_JIFFIES( |
| 653 | channel->expiration_period_ms)); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | spin_unlock_irqrestore(&device->spinlock, flags); |
| 657 | } |
| 658 | |
| 659 | /*** GPIO functions ***/ |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 660 | static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device, |
| 661 | const struct msm_gpio *table, |
| 662 | int size) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 663 | { |
| 664 | int rc = 0; |
| 665 | int i; |
| 666 | const struct msm_gpio *g; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 667 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 668 | for (i = size-1; i >= 0; i--) { |
| 669 | int tmp; |
| 670 | g = table + i; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 671 | |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 672 | /* don't use sync GPIO when not working in mode 2 */ |
| 673 | if ((tsif_device->mode != TSPP_TSIF_MODE_2) && |
| 674 | (strnstr(g->label, "sync", strlen(g->label)) != NULL)) |
| 675 | continue; |
| 676 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 677 | tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg), |
| 678 | 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), |
| 679 | GPIO_CFG_DISABLE); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 680 | if (tmp) { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 681 | pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n", |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 682 | g->gpio_cfg, g->label ?: "?", rc); |
| 683 | pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n", |
| 684 | GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg), |
| 685 | GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg), |
| 686 | GPIO_DRVSTR(g->gpio_cfg)); |
| 687 | if (!rc) |
| 688 | rc = tmp; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return rc; |
| 693 | } |
| 694 | |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 695 | static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device, |
| 696 | const struct msm_gpio *table, |
| 697 | int size) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 698 | { |
| 699 | int rc; |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 700 | int i; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 701 | const struct msm_gpio *g; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 702 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 703 | for (i = 0; i < size; i++) { |
| 704 | g = table + i; |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 705 | |
| 706 | /* don't use sync GPIO when not working in mode 2 */ |
| 707 | if ((tsif_device->mode != TSPP_TSIF_MODE_2) && |
| 708 | (strnstr(g->label, "sync", strlen(g->label)) != NULL)) |
| 709 | continue; |
| 710 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 711 | rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE); |
| 712 | if (rc) { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 713 | pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n", |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 714 | g->gpio_cfg, g->label ?: "?", rc); |
| 715 | pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n", |
| 716 | GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg), |
| 717 | GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg), |
| 718 | GPIO_DRVSTR(g->gpio_cfg)); |
| 719 | goto err; |
| 720 | } |
| 721 | } |
| 722 | return 0; |
| 723 | err: |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 724 | tspp_gpios_disable(tsif_device, table, i); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 725 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 726 | return rc; |
| 727 | } |
| 728 | |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 729 | |
| 730 | static int tspp_config_gpios(struct tspp_device *device, |
| 731 | enum tspp_source source, |
| 732 | int enable) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 733 | { |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 734 | const struct msm_gpio *table; |
| 735 | struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data; |
| 736 | int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES); |
| 737 | int i = 0; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 738 | |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 739 | if (num_gpios != TSPP_GPIOS_PER_TSIF) { |
| 740 | pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n", |
| 741 | __func__, num_gpios, TSPP_GPIOS_PER_TSIF); |
| 742 | return -EINVAL; |
| 743 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 744 | |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 745 | /* |
| 746 | * Note: this code assumes that the GPIO definitions in the |
| 747 | * pdata->gpios table are according to the TSIF instance number, |
| 748 | * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc. |
| 749 | */ |
| 750 | switch (source) { |
| 751 | case TSPP_SOURCE_TSIF0: |
| 752 | i = 0; |
| 753 | break; |
| 754 | case TSPP_SOURCE_TSIF1: |
| 755 | i = 1; |
| 756 | break; |
| 757 | default: |
| 758 | pr_err("tspp %s: invalid source\n", __func__); |
| 759 | return -EINVAL; |
| 760 | } |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 761 | |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 762 | table = pdata->gpios + (i * num_gpios); |
| 763 | if (enable) |
| 764 | return tspp_gpios_enable(&device->tsif[i], table, num_gpios); |
| 765 | else |
| 766 | return tspp_gpios_disable(&device->tsif[i], table, num_gpios); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 769 | /*** Clock functions ***/ |
| 770 | static int tspp_clock_start(struct tspp_device *device) |
| 771 | { |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 772 | int rc; |
| 773 | |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 774 | if (device == NULL) { |
| 775 | pr_err("tspp: Can't start clocks, invalid device\n"); |
| 776 | return -EINVAL; |
| 777 | } |
| 778 | |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 779 | if (device->tsif_bus_client) { |
| 780 | rc = msm_bus_scale_client_update_request( |
| 781 | device->tsif_bus_client, 1); |
| 782 | if (rc) { |
| 783 | pr_err("tspp: Can't enable bus\n"); |
| 784 | return -EBUSY; |
| 785 | } |
| 786 | } |
| 787 | |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 788 | if (device->tsif_vreg) { |
| 789 | rc = regulator_set_voltage(device->tsif_vreg, |
| 790 | RPM_REGULATOR_CORNER_SUPER_TURBO, |
| 791 | RPM_REGULATOR_CORNER_SUPER_TURBO); |
| 792 | if (rc) { |
| 793 | pr_err("Unable to set CX voltage.\n"); |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 794 | if (device->tsif_bus_client) |
| 795 | msm_bus_scale_client_update_request( |
| 796 | device->tsif_bus_client, 0); |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 797 | return rc; |
| 798 | } |
| 799 | } |
| 800 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 801 | if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) { |
| 802 | pr_err("tspp: Can't start pclk"); |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 803 | |
| 804 | if (device->tsif_vreg) { |
| 805 | regulator_set_voltage(device->tsif_vreg, |
Hamad Kadmany | fde2938 | 2013-06-26 08:03:51 +0300 | [diff] [blame] | 806 | RPM_REGULATOR_CORNER_NONE, |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 807 | RPM_REGULATOR_CORNER_SUPER_TURBO); |
| 808 | } |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 809 | |
| 810 | if (device->tsif_bus_client) |
| 811 | msm_bus_scale_client_update_request( |
| 812 | device->tsif_bus_client, 0); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 813 | return -EBUSY; |
| 814 | } |
| 815 | |
| 816 | if (device->tsif_ref_clk && |
| 817 | clk_prepare_enable(device->tsif_ref_clk) != 0) { |
| 818 | pr_err("tspp: Can't start ref clk"); |
| 819 | clk_disable_unprepare(device->tsif_pclk); |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 820 | if (device->tsif_vreg) { |
| 821 | regulator_set_voltage(device->tsif_vreg, |
Hamad Kadmany | fde2938 | 2013-06-26 08:03:51 +0300 | [diff] [blame] | 822 | RPM_REGULATOR_CORNER_NONE, |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 823 | RPM_REGULATOR_CORNER_SUPER_TURBO); |
| 824 | } |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 825 | |
| 826 | if (device->tsif_bus_client) |
| 827 | msm_bus_scale_client_update_request( |
| 828 | device->tsif_bus_client, 0); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 829 | return -EBUSY; |
| 830 | } |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | static void tspp_clock_stop(struct tspp_device *device) |
| 836 | { |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 837 | int rc; |
| 838 | |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 839 | if (device == NULL) { |
| 840 | pr_err("tspp: Can't stop clocks, invalid device\n"); |
| 841 | return; |
| 842 | } |
| 843 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 844 | if (device->tsif_pclk) |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 845 | clk_disable_unprepare(device->tsif_pclk); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 846 | |
| 847 | if (device->tsif_ref_clk) |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 848 | clk_disable_unprepare(device->tsif_ref_clk); |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 849 | |
| 850 | if (device->tsif_vreg) { |
| 851 | rc = regulator_set_voltage(device->tsif_vreg, |
Hamad Kadmany | fde2938 | 2013-06-26 08:03:51 +0300 | [diff] [blame] | 852 | RPM_REGULATOR_CORNER_NONE, |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 853 | RPM_REGULATOR_CORNER_SUPER_TURBO); |
| 854 | if (rc) |
| 855 | pr_err("Unable to set CX voltage.\n"); |
| 856 | } |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 857 | |
| 858 | if (device->tsif_bus_client) { |
| 859 | rc = msm_bus_scale_client_update_request( |
| 860 | device->tsif_bus_client, 0); |
| 861 | if (rc) |
| 862 | pr_err("tspp: Can't disable bus\n"); |
| 863 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 866 | /*** TSIF functions ***/ |
| 867 | static int tspp_start_tsif(struct tspp_tsif_device *tsif_device) |
| 868 | { |
| 869 | int start_hardware = 0; |
| 870 | u32 ctl; |
| 871 | |
| 872 | if (tsif_device->ref_count == 0) { |
| 873 | start_hardware = 1; |
| 874 | } else if (tsif_device->ref_count > 0) { |
| 875 | ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF); |
| 876 | if ((ctl & TSIF_STS_CTL_START) != 1) { |
| 877 | /* this hardware should already be running */ |
| 878 | pr_warn("tspp: tsif hw not started but ref count > 0"); |
| 879 | start_hardware = 1; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | if (start_hardware) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 884 | ctl = TSIF_STS_CTL_EN_IRQ | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 885 | TSIF_STS_CTL_EN_DM; |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 886 | |
| 887 | if (tsif_device->clock_inverse) |
| 888 | ctl |= TSIF_STS_CTL_INV_CLOCK; |
| 889 | |
| 890 | if (tsif_device->data_inverse) |
| 891 | ctl |= TSIF_STS_CTL_INV_DATA; |
| 892 | |
| 893 | if (tsif_device->sync_inverse) |
| 894 | ctl |= TSIF_STS_CTL_INV_SYNC; |
| 895 | |
| 896 | if (tsif_device->enable_inverse) |
| 897 | ctl |= TSIF_STS_CTL_INV_ENABLE; |
| 898 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 899 | switch (tsif_device->mode) { |
| 900 | case TSPP_TSIF_MODE_LOOPBACK: |
| 901 | ctl |= TSIF_STS_CTL_EN_NULL | |
| 902 | TSIF_STS_CTL_EN_ERROR | |
| 903 | TSIF_STS_CTL_TEST_MODE; |
| 904 | break; |
| 905 | case TSPP_TSIF_MODE_1: |
| 906 | ctl |= TSIF_STS_CTL_EN_TIME_LIM | |
| 907 | TSIF_STS_CTL_EN_TCR; |
| 908 | break; |
| 909 | case TSPP_TSIF_MODE_2: |
| 910 | ctl |= TSIF_STS_CTL_EN_TIME_LIM | |
| 911 | TSIF_STS_CTL_EN_TCR | |
| 912 | TSIF_STS_CTL_MODE_2; |
| 913 | break; |
| 914 | default: |
| 915 | pr_warn("tspp: unknown tsif mode 0x%x", |
| 916 | tsif_device->mode); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 917 | } |
| 918 | writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF); |
| 919 | writel_relaxed(tsif_device->time_limit, |
| 920 | tsif_device->base + TSIF_TIME_LIMIT_OFF); |
| 921 | wmb(); |
| 922 | writel_relaxed(ctl | TSIF_STS_CTL_START, |
| 923 | tsif_device->base + TSIF_STS_CTL_OFF); |
| 924 | wmb(); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 927 | ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF); |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 928 | if (!(ctl & TSIF_STS_CTL_START)) |
| 929 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 930 | |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 931 | tsif_device->ref_count++; |
| 932 | return 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device) |
| 936 | { |
| 937 | if (tsif_device->ref_count == 0) |
| 938 | return; |
| 939 | |
| 940 | tsif_device->ref_count--; |
| 941 | |
| 942 | if (tsif_device->ref_count == 0) { |
| 943 | writel_relaxed(TSIF_STS_CTL_STOP, |
| 944 | tsif_device->base + TSIF_STS_CTL_OFF); |
| 945 | wmb(); |
| 946 | } |
| 947 | } |
| 948 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 949 | /*** local TSPP functions ***/ |
| 950 | static int tspp_channels_in_use(struct tspp_device *pdev) |
| 951 | { |
| 952 | int i; |
| 953 | int count = 0; |
| 954 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) |
| 955 | count += (pdev->channels[i].used ? 1 : 0); |
| 956 | |
| 957 | return count; |
| 958 | } |
| 959 | |
| 960 | static struct tspp_device *tspp_find_by_id(int id) |
| 961 | { |
| 962 | struct tspp_device *dev; |
| 963 | list_for_each_entry(dev, &tspp_devices, devlist) { |
| 964 | if (dev->pdev->id == id) |
| 965 | return dev; |
| 966 | } |
| 967 | return NULL; |
| 968 | } |
| 969 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 970 | static int tspp_get_key_entry(void) |
| 971 | { |
| 972 | int i; |
| 973 | for (i = 0; i < TSPP_NUM_KEYS; i++) { |
| 974 | if (!(tspp_key_entry & (1 << i))) { |
| 975 | tspp_key_entry |= (1 << i); |
| 976 | return i; |
| 977 | } |
| 978 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 979 | return 1 < TSPP_NUM_KEYS; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static void tspp_free_key_entry(int entry) |
| 983 | { |
| 984 | if (entry > TSPP_NUM_KEYS) { |
| 985 | pr_err("tspp_free_key_entry: index out of bounds"); |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | tspp_key_entry &= ~(1 << entry); |
| 990 | } |
| 991 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 992 | static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc, |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 993 | u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 994 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 995 | if (size < TSPP_MIN_BUFFER_SIZE || |
| 996 | size > TSPP_MAX_BUFFER_SIZE) { |
| 997 | pr_err("tspp: bad buffer size %i", size); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 998 | return -ENOMEM; |
| 999 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1000 | |
| 1001 | if (alloc) { |
| 1002 | TSPP_DEBUG("tspp using alloc function"); |
| 1003 | desc->virt_base = alloc(channel_id, size, |
| 1004 | &desc->phys_base, user); |
| 1005 | } else { |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 1006 | if (!dma_pool) |
| 1007 | desc->virt_base = dma_alloc_coherent(NULL, size, |
| 1008 | &desc->phys_base, GFP_KERNEL); |
| 1009 | else |
| 1010 | desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL, |
| 1011 | &desc->phys_base); |
| 1012 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1013 | if (desc->virt_base == 0) { |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 1014 | pr_err("tspp: dma buffer allocation failed %i\n", size); |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1015 | return -ENOMEM; |
| 1016 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | desc->size = size; |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static int tspp_queue_buffer(struct tspp_channel *channel, |
| 1024 | struct tspp_mem_buffer *buffer) |
| 1025 | { |
| 1026 | int rc; |
| 1027 | u32 flags = 0; |
| 1028 | |
| 1029 | /* make sure the interrupt frequency is valid */ |
| 1030 | if (channel->int_freq < 1) |
| 1031 | channel->int_freq = 1; |
| 1032 | |
| 1033 | /* generate interrupt according to requested frequency */ |
| 1034 | if (buffer->desc.id % channel->int_freq == channel->int_freq-1) |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1035 | flags = SPS_IOVEC_FLAG_INT; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* start the transfer */ |
| 1038 | rc = sps_transfer_one(channel->pipe, |
| 1039 | buffer->sps.phys_base, |
| 1040 | buffer->sps.size, |
| 1041 | channel->pdev, |
| 1042 | flags); |
| 1043 | if (rc < 0) |
| 1044 | return rc; |
| 1045 | |
| 1046 | buffer->state = TSPP_BUF_STATE_WAITING; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | static int tspp_global_reset(struct tspp_device *pdev) |
| 1052 | { |
| 1053 | u32 i, val; |
| 1054 | |
| 1055 | /* stop all TSIFs */ |
| 1056 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) { |
| 1057 | pdev->tsif[i].ref_count = 1; /* allows stopping hw */ |
| 1058 | tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */ |
| 1059 | pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT; |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1060 | pdev->tsif[i].clock_inverse = 0; |
| 1061 | pdev->tsif[i].data_inverse = 0; |
| 1062 | pdev->tsif[i].sync_inverse = 0; |
| 1063 | pdev->tsif[i].enable_inverse = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1064 | } |
| 1065 | writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST); |
| 1066 | wmb(); |
| 1067 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1068 | /* TSPP tables */ |
| 1069 | for (i = 0; i < TSPP_FILTER_TABLES; i++) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1070 | memset(pdev->filters[i], |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1071 | 0, sizeof(struct tspp_pid_filter_table)); |
| 1072 | |
| 1073 | /* disable all filters */ |
| 1074 | val = (2 << TSPP_NUM_CHANNELS) - 1; |
| 1075 | writel_relaxed(val, pdev->base + TSPP_PS_DISABLE); |
| 1076 | |
| 1077 | /* TSPP registers */ |
| 1078 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1079 | writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT, |
| 1080 | pdev->base + TSPP_CONTROL); |
| 1081 | wmb(); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1082 | memset(pdev->tspp_global_performance, 0, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1083 | sizeof(struct tspp_global_performance_regs)); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1084 | memset(pdev->tspp_pipe_context, 0, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1085 | sizeof(struct tspp_pipe_context_regs)); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1086 | memset(pdev->tspp_pipe_performance, 0, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1087 | sizeof(struct tspp_pipe_performance_regs)); |
| 1088 | wmb(); |
| 1089 | writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT, |
| 1090 | pdev->base + TSPP_CONTROL); |
| 1091 | wmb(); |
| 1092 | |
| 1093 | val = readl_relaxed(pdev->base + TSPP_CONFIG); |
| 1094 | val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK | |
| 1095 | TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK | |
| 1096 | TSPP_CONFIG_PS_CONT_ERR_MASK); |
| 1097 | TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH); |
| 1098 | writel_relaxed(val, pdev->base + TSPP_CONFIG); |
Hamad Kadmany | 6bac783 | 2012-12-20 18:30:40 +0200 | [diff] [blame] | 1099 | writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1100 | writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR); |
| 1101 | writel_relaxed(0, pdev->base + TSPP_RST); |
| 1102 | wmb(); |
| 1103 | |
| 1104 | tspp_key_entry = 0; |
| 1105 | |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1109 | static int tspp_select_source(u32 dev, u32 channel_id, |
| 1110 | struct tspp_select_source *src) |
| 1111 | { |
| 1112 | /* make sure the requested src id is in bounds */ |
| 1113 | if (src->source > TSPP_SOURCE_MEM) { |
| 1114 | pr_err("tspp source out of bounds"); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | |
| 1118 | /* open the stream */ |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1119 | tspp_open_stream(dev, channel_id, src); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv) |
| 1125 | { |
| 1126 | struct tspp_device *pdev = channel->pdev; |
| 1127 | |
| 1128 | writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0)); |
| 1129 | writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1)); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static int tspp_set_system_keys(struct tspp_channel *channel, |
| 1134 | struct tspp_system_keys *keys) |
| 1135 | { |
| 1136 | int i; |
| 1137 | struct tspp_device *pdev = channel->pdev; |
| 1138 | |
| 1139 | for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++) |
| 1140 | writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i)); |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static int tspp_channel_init(struct tspp_channel *channel, |
| 1146 | struct tspp_device *pdev) |
| 1147 | { |
| 1148 | channel->cdev.owner = THIS_MODULE; |
| 1149 | cdev_init(&channel->cdev, &tspp_fops); |
| 1150 | channel->pdev = pdev; |
| 1151 | channel->data = NULL; |
| 1152 | channel->read = NULL; |
| 1153 | channel->waiting = NULL; |
| 1154 | channel->locked = NULL; |
| 1155 | channel->id = MINOR(tspp_minor); |
| 1156 | channel->used = 0; |
| 1157 | channel->buffer_size = TSPP_MIN_BUFFER_SIZE; |
| 1158 | channel->max_buffers = TSPP_NUM_BUFFERS; |
| 1159 | channel->buffer_count = 0; |
| 1160 | channel->filter_count = 0; |
| 1161 | channel->int_freq = 1; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1162 | channel->src = TSPP_SOURCE_NONE; |
| 1163 | channel->mode = TSPP_MODE_DISABLED; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1164 | channel->notifier = NULL; |
| 1165 | channel->notify_data = NULL; |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1166 | channel->expiration_period_ms = 0; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1167 | channel->memfree = NULL; |
| 1168 | channel->user_info = NULL; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1169 | init_waitqueue_head(&channel->in_queue); |
| 1170 | |
| 1171 | if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) { |
| 1172 | pr_err("tspp: cdev_add failed"); |
| 1173 | return -EBUSY; |
| 1174 | } |
| 1175 | |
| 1176 | channel->dd = device_create(tspp_class, NULL, channel->cdev.dev, |
| 1177 | channel, "tspp%02d", channel->id); |
| 1178 | if (IS_ERR(channel->dd)) { |
| 1179 | pr_err("tspp: device_create failed: %i", |
| 1180 | (int)PTR_ERR(channel->dd)); |
| 1181 | cdev_del(&channel->cdev); |
| 1182 | return -EBUSY; |
| 1183 | } |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int tspp_set_buffer_size(struct tspp_channel *channel, |
| 1189 | struct tspp_buffer *buf) |
| 1190 | { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1191 | if (channel->buffer_count > 0) { |
| 1192 | pr_err("tspp: cannot set buffer size - buffers already allocated\n"); |
| 1193 | return -EPERM; |
| 1194 | } |
| 1195 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1196 | if (buf->size < TSPP_MIN_BUFFER_SIZE) |
| 1197 | channel->buffer_size = TSPP_MIN_BUFFER_SIZE; |
| 1198 | else if (buf->size > TSPP_MAX_BUFFER_SIZE) |
| 1199 | channel->buffer_size = TSPP_MAX_BUFFER_SIZE; |
| 1200 | else |
| 1201 | channel->buffer_size = buf->size; |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | static void tspp_set_tsif_mode(struct tspp_channel *channel, |
| 1207 | enum tspp_tsif_mode mode) |
| 1208 | { |
| 1209 | int index; |
| 1210 | |
| 1211 | switch (channel->src) { |
| 1212 | case TSPP_SOURCE_TSIF0: |
| 1213 | index = 0; |
| 1214 | break; |
| 1215 | case TSPP_SOURCE_TSIF1: |
| 1216 | index = 1; |
| 1217 | break; |
| 1218 | default: |
| 1219 | pr_warn("tspp: can't set mode for non-tsif source %d", |
| 1220 | channel->src); |
| 1221 | return; |
| 1222 | } |
| 1223 | channel->pdev->tsif[index].mode = mode; |
| 1224 | } |
| 1225 | |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1226 | static void tspp_set_signal_inversion(struct tspp_channel *channel, |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1227 | int clock_inverse, int data_inverse, |
| 1228 | int sync_inverse, int enable_inverse) |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1229 | { |
| 1230 | int index; |
| 1231 | |
| 1232 | switch (channel->src) { |
| 1233 | case TSPP_SOURCE_TSIF0: |
| 1234 | index = 0; |
| 1235 | break; |
| 1236 | case TSPP_SOURCE_TSIF1: |
| 1237 | index = 1; |
| 1238 | break; |
| 1239 | default: |
| 1240 | return; |
| 1241 | } |
| 1242 | channel->pdev->tsif[index].clock_inverse = clock_inverse; |
| 1243 | channel->pdev->tsif[index].data_inverse = data_inverse; |
| 1244 | channel->pdev->tsif[index].sync_inverse = sync_inverse; |
| 1245 | channel->pdev->tsif[index].enable_inverse = enable_inverse; |
| 1246 | } |
| 1247 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1248 | static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode) |
| 1249 | { |
| 1250 | u32 alignment; |
| 1251 | |
| 1252 | switch (mode) { |
| 1253 | case TSPP_MODE_RAW: |
| 1254 | /* must be a multiple of 192 */ |
| 1255 | alignment = (TSPP_PACKET_LENGTH + 4); |
| 1256 | if (size % alignment) |
| 1257 | return 0; |
| 1258 | return 1; |
| 1259 | |
| 1260 | case TSPP_MODE_RAW_NO_SUFFIX: |
| 1261 | /* must be a multiple of 188 */ |
| 1262 | alignment = TSPP_PACKET_LENGTH; |
| 1263 | if (size % alignment) |
| 1264 | return 0; |
| 1265 | return 1; |
| 1266 | |
| 1267 | case TSPP_MODE_DISABLED: |
| 1268 | case TSPP_MODE_PES: |
| 1269 | default: |
| 1270 | /* no alignment requirement */ |
| 1271 | return 1; |
| 1272 | } |
| 1273 | |
| 1274 | } |
| 1275 | |
| 1276 | static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode) |
| 1277 | { |
| 1278 | u32 new_size; |
| 1279 | u32 alignment; |
| 1280 | |
| 1281 | switch (mode) { |
| 1282 | case TSPP_MODE_RAW: |
| 1283 | /* must be a multiple of 192 */ |
| 1284 | alignment = (TSPP_PACKET_LENGTH + 4); |
| 1285 | break; |
| 1286 | |
| 1287 | case TSPP_MODE_RAW_NO_SUFFIX: |
| 1288 | /* must be a multiple of 188 */ |
| 1289 | alignment = TSPP_PACKET_LENGTH; |
| 1290 | break; |
| 1291 | |
| 1292 | case TSPP_MODE_DISABLED: |
| 1293 | case TSPP_MODE_PES: |
| 1294 | default: |
| 1295 | /* no alignment requirement - give the user what he asks for */ |
| 1296 | alignment = 1; |
| 1297 | break; |
| 1298 | } |
| 1299 | /* align up */ |
| 1300 | new_size = (((size + alignment - 1) / alignment) * alignment); |
| 1301 | return new_size; |
| 1302 | } |
| 1303 | |
| 1304 | static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel) |
| 1305 | { |
| 1306 | int i; |
| 1307 | struct tspp_mem_buffer *pbuf, *temp; |
| 1308 | |
| 1309 | pbuf = channel->data; |
| 1310 | for (i = 0; i < channel->buffer_count; i++) { |
| 1311 | if (pbuf->desc.phys_base) { |
| 1312 | if (channel->memfree) { |
| 1313 | channel->memfree(channel_id, |
| 1314 | pbuf->desc.size, |
| 1315 | pbuf->desc.virt_base, |
| 1316 | pbuf->desc.phys_base, |
| 1317 | channel->user_info); |
| 1318 | } else { |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 1319 | if (!channel->dma_pool) |
| 1320 | dma_free_coherent(NULL, |
| 1321 | pbuf->desc.size, |
| 1322 | pbuf->desc.virt_base, |
| 1323 | pbuf->desc.phys_base); |
| 1324 | else |
| 1325 | dma_pool_free(channel->dma_pool, |
| 1326 | pbuf->desc.virt_base, |
| 1327 | pbuf->desc.phys_base); |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1328 | } |
| 1329 | pbuf->desc.phys_base = 0; |
| 1330 | } |
| 1331 | pbuf->desc.virt_base = 0; |
| 1332 | pbuf->state = TSPP_BUF_STATE_EMPTY; |
| 1333 | temp = pbuf; |
| 1334 | pbuf = pbuf->next; |
| 1335 | kfree(temp); |
| 1336 | } |
| 1337 | } |
| 1338 | |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1339 | static int msm_tspp_req_irqs(struct tspp_device *device) |
| 1340 | { |
| 1341 | int rc; |
| 1342 | int i; |
| 1343 | int j; |
| 1344 | |
| 1345 | rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED, |
| 1346 | dev_name(&device->pdev->dev), device); |
| 1347 | if (rc) { |
| 1348 | dev_err(&device->pdev->dev, |
| 1349 | "failed to request TSPP IRQ %d : %d", |
| 1350 | device->tspp_irq, rc); |
| 1351 | return rc; |
| 1352 | } |
| 1353 | |
| 1354 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) { |
| 1355 | rc = request_irq(device->tsif[i].tsif_irq, |
| 1356 | tsif_isr, IRQF_SHARED, dev_name(&device->pdev->dev), |
| 1357 | &device->tsif[i]); |
| 1358 | if (rc) { |
| 1359 | dev_err(&device->pdev->dev, |
| 1360 | "failed to request TSIF%d IRQ: %d", |
| 1361 | i, rc); |
| 1362 | goto failed; |
| 1363 | } |
| 1364 | } |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1365 | device->req_irqs = true; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1366 | return 0; |
| 1367 | |
| 1368 | failed: |
| 1369 | free_irq(device->tspp_irq, device); |
| 1370 | for (j = 0; j < i; j++) |
| 1371 | free_irq(device->tsif[j].tsif_irq, device); |
| 1372 | |
| 1373 | return rc; |
| 1374 | } |
| 1375 | |
| 1376 | static inline void msm_tspp_free_irqs(struct tspp_device *device) |
| 1377 | { |
| 1378 | int i; |
| 1379 | |
| 1380 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) { |
| 1381 | if (device->tsif[i].tsif_irq) |
| 1382 | free_irq(device->tsif[i].tsif_irq, &device->tsif[i]); |
| 1383 | } |
| 1384 | |
| 1385 | if (device->tspp_irq) |
| 1386 | free_irq(device->tspp_irq, device); |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1387 | device->req_irqs = false; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1388 | } |
| 1389 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1390 | /*** TSPP API functions ***/ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1391 | |
| 1392 | /** |
| 1393 | * tspp_open_stream - open a TSPP stream for use. |
| 1394 | * |
| 1395 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 1396 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 1397 | * @source: stream source parameters. |
| 1398 | * |
| 1399 | * Return error status |
| 1400 | * |
| 1401 | */ |
| 1402 | int tspp_open_stream(u32 dev, u32 channel_id, |
| 1403 | struct tspp_select_source *source) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1404 | { |
| 1405 | u32 val; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1406 | int rc; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1407 | struct tspp_device *pdev; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1408 | struct tspp_channel *channel; |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1409 | bool req_irqs = false; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1410 | |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1411 | TSPP_DEBUG("tspp_open_stream %i %i %i %i", |
| 1412 | dev, channel_id, source->source, source->mode); |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1413 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1414 | if (dev >= TSPP_MAX_DEVICES) { |
| 1415 | pr_err("tspp: device id out of range"); |
| 1416 | return -ENODEV; |
| 1417 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1418 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1419 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1420 | pr_err("tspp: channel id out of range"); |
| 1421 | return -ECHRNG; |
| 1422 | } |
| 1423 | |
| 1424 | pdev = tspp_find_by_id(dev); |
| 1425 | if (!pdev) { |
| 1426 | pr_err("tspp_str: can't find device %i", dev); |
| 1427 | return -ENODEV; |
| 1428 | } |
| 1429 | channel = &pdev->channels[channel_id]; |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1430 | channel->src = source->source; |
| 1431 | tspp_set_tsif_mode(channel, source->mode); |
| 1432 | tspp_set_signal_inversion(channel, source->clk_inverse, |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1433 | source->data_inverse, source->sync_inverse, |
| 1434 | source->enable_inverse); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1435 | |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1436 | /* Request IRQ resources on first open */ |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1437 | if (!pdev->req_irqs && (source->source == TSPP_SOURCE_TSIF0 || |
| 1438 | source->source == TSPP_SOURCE_TSIF1)) { |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1439 | rc = msm_tspp_req_irqs(pdev); |
| 1440 | if (rc) { |
| 1441 | pr_err("tspp: error requesting irqs\n"); |
| 1442 | return rc; |
| 1443 | } |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1444 | req_irqs = true; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1445 | } |
| 1446 | |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1447 | switch (source->source) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1448 | case TSPP_SOURCE_TSIF0: |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1449 | if (tspp_config_gpios(pdev, channel->src, 1) != 0) { |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1450 | rc = -EBUSY; |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1451 | pr_err("tspp: error enabling tsif0 GPIOs\n"); |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1452 | goto free_irq; |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1453 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1454 | /* make sure TSIF0 is running & enabled */ |
| 1455 | if (tspp_start_tsif(&pdev->tsif[0]) != 0) { |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1456 | rc = -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1457 | pr_err("tspp: error starting tsif0"); |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1458 | goto free_irq; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1459 | } |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 1460 | if (pdev->tsif[0].ref_count == 1) { |
| 1461 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1462 | writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS, |
| 1463 | pdev->base + TSPP_CONTROL); |
| 1464 | wmb(); |
| 1465 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1466 | break; |
| 1467 | case TSPP_SOURCE_TSIF1: |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1468 | if (tspp_config_gpios(pdev, channel->src, 1) != 0) { |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1469 | rc = -EBUSY; |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1470 | pr_err("tspp: error enabling tsif1 GPIOs\n"); |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1471 | goto free_irq; |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1472 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1473 | /* make sure TSIF1 is running & enabled */ |
| 1474 | if (tspp_start_tsif(&pdev->tsif[1]) != 0) { |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1475 | rc = -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1476 | pr_err("tspp: error starting tsif1"); |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1477 | goto free_irq; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1478 | } |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 1479 | if (pdev->tsif[1].ref_count == 1) { |
| 1480 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1481 | writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS, |
| 1482 | pdev->base + TSPP_CONTROL); |
| 1483 | wmb(); |
| 1484 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1485 | break; |
| 1486 | case TSPP_SOURCE_MEM: |
| 1487 | break; |
| 1488 | default: |
Hamad Kadmany | 92705b3 | 2012-10-23 14:15:41 +0200 | [diff] [blame] | 1489 | pr_err("tspp: channel %i invalid source %i", |
| 1490 | channel->id, source->source); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1491 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1492 | } |
| 1493 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1494 | return 0; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1495 | |
| 1496 | free_irq: |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1497 | /* Free irqs only if were requested during opening of this stream */ |
| 1498 | if (req_irqs) |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1499 | msm_tspp_free_irqs(pdev); |
| 1500 | return rc; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1501 | } |
| 1502 | EXPORT_SYMBOL(tspp_open_stream); |
| 1503 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1504 | /** |
| 1505 | * tspp_close_stream - close a TSPP stream. |
| 1506 | * |
| 1507 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 1508 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 1509 | * |
| 1510 | * Return error status |
| 1511 | * |
| 1512 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1513 | int tspp_close_stream(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1514 | { |
| 1515 | u32 val; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1516 | u32 prev_ref_count = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1517 | struct tspp_device *pdev; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1518 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1519 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1520 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1521 | pr_err("tspp: channel id out of range"); |
| 1522 | return -ECHRNG; |
| 1523 | } |
| 1524 | pdev = tspp_find_by_id(dev); |
| 1525 | if (!pdev) { |
| 1526 | pr_err("tspp_cs: can't find device %i", dev); |
| 1527 | return -EBUSY; |
| 1528 | } |
| 1529 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1530 | |
| 1531 | switch (channel->src) { |
| 1532 | case TSPP_SOURCE_TSIF0: |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 1533 | prev_ref_count = pdev->tsif[0].ref_count; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1534 | tspp_stop_tsif(&pdev->tsif[0]); |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1535 | if (tspp_config_gpios(pdev, channel->src, 0) != 0) |
| 1536 | pr_err("tspp: error disabling tsif0 GPIOs\n"); |
| 1537 | |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 1538 | if (prev_ref_count == 1) { |
| 1539 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1540 | writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS, |
| 1541 | pdev->base + TSPP_CONTROL); |
| 1542 | wmb(); |
| 1543 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1544 | break; |
| 1545 | case TSPP_SOURCE_TSIF1: |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 1546 | prev_ref_count = pdev->tsif[1].ref_count; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1547 | tspp_stop_tsif(&pdev->tsif[1]); |
Liron Kuch | 275c0b3 | 2013-02-10 15:19:32 +0200 | [diff] [blame] | 1548 | if (tspp_config_gpios(pdev, channel->src, 0) != 0) |
| 1549 | pr_err("tspp: error disabling tsif0 GPIOs\n"); |
| 1550 | |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 1551 | if (prev_ref_count == 1) { |
| 1552 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1553 | writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS, |
| 1554 | pdev->base + TSPP_CONTROL); |
| 1555 | wmb(); |
| 1556 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1557 | break; |
| 1558 | case TSPP_SOURCE_MEM: |
| 1559 | break; |
| 1560 | case TSPP_SOURCE_NONE: |
| 1561 | break; |
| 1562 | } |
| 1563 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1564 | channel->src = TSPP_SOURCE_NONE; |
Gilad Broner | 6049ea3 | 2014-04-10 09:48:44 +0300 | [diff] [blame] | 1565 | |
| 1566 | /* Free requested interrupts to save power */ |
| 1567 | if ((pdev->tsif[0].ref_count + pdev->tsif[1].ref_count) == 0 && |
| 1568 | prev_ref_count) |
| 1569 | msm_tspp_free_irqs(pdev); |
| 1570 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | EXPORT_SYMBOL(tspp_close_stream); |
| 1574 | |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1575 | static int tspp_init_sps_device(struct tspp_device *dev) |
| 1576 | { |
| 1577 | int ret; |
| 1578 | |
| 1579 | ret = sps_register_bam_device(&dev->bam_props, &dev->bam_handle); |
| 1580 | if (ret) { |
| 1581 | pr_err("tspp: failed to register bam device, err-%d\n", ret); |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
| 1585 | ret = sps_device_reset(dev->bam_handle); |
| 1586 | if (ret) { |
| 1587 | sps_deregister_bam_device(dev->bam_handle); |
| 1588 | pr_err("tspp: error resetting bam device, err=%d\n", ret); |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1595 | /** |
| 1596 | * tspp_open_channel - open a TSPP channel. |
| 1597 | * |
| 1598 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 1599 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 1600 | * |
| 1601 | * Return error status |
| 1602 | * |
| 1603 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1604 | int tspp_open_channel(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1605 | { |
| 1606 | int rc = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1607 | struct sps_connect *config; |
| 1608 | struct sps_register_event *event; |
| 1609 | struct tspp_channel *channel; |
| 1610 | struct tspp_device *pdev; |
| 1611 | |
| 1612 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1613 | pr_err("tspp: channel id out of range"); |
| 1614 | return -ECHRNG; |
| 1615 | } |
| 1616 | pdev = tspp_find_by_id(dev); |
| 1617 | if (!pdev) { |
| 1618 | pr_err("tspp_oc: can't find device %i", dev); |
| 1619 | return -ENODEV; |
| 1620 | } |
| 1621 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1622 | |
| 1623 | if (channel->used) { |
| 1624 | pr_err("tspp channel already in use"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1625 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1628 | config = &channel->config; |
| 1629 | event = &channel->event; |
| 1630 | |
| 1631 | /* start the clocks if needed */ |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 1632 | if (tspp_channels_in_use(pdev) == 0) { |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 1633 | rc = tspp_clock_start(pdev); |
| 1634 | if (rc) |
| 1635 | return rc; |
| 1636 | |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1637 | if (pdev->bam_handle == SPS_DEV_HANDLE_INVALID) { |
| 1638 | rc = tspp_init_sps_device(pdev); |
| 1639 | if (rc) { |
| 1640 | pr_err("tspp: failed to init sps device, err=%d\n", |
| 1641 | rc); |
| 1642 | tspp_clock_stop(pdev); |
| 1643 | return rc; |
| 1644 | } |
| 1645 | } |
| 1646 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1647 | wake_lock(&pdev->wake_lock); |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 1648 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1649 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1650 | /* mark it as used */ |
| 1651 | channel->used = 1; |
| 1652 | |
| 1653 | /* start the bam */ |
| 1654 | channel->pipe = sps_alloc_endpoint(); |
| 1655 | if (channel->pipe == 0) { |
| 1656 | pr_err("tspp: error allocating endpoint"); |
| 1657 | rc = -ENOMEM; |
| 1658 | goto err_sps_alloc; |
| 1659 | } |
| 1660 | |
| 1661 | /* get default configuration */ |
| 1662 | sps_get_config(channel->pipe, config); |
| 1663 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1664 | config->source = pdev->bam_handle; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1665 | config->destination = SPS_DEV_HANDLE_MEM; |
| 1666 | config->mode = SPS_MODE_SRC; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1667 | config->options = |
| 1668 | SPS_O_AUTO_ENABLE | /* connection is auto-enabled */ |
| 1669 | SPS_O_STREAMING | /* streaming mode */ |
| 1670 | SPS_O_DESC_DONE | /* interrupt on end of descriptor */ |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1671 | SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */ |
| 1672 | SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1673 | config->src_pipe_index = channel->id; |
| 1674 | config->desc.size = |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1675 | TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1676 | config->desc.base = dma_alloc_coherent(NULL, |
| 1677 | config->desc.size, |
| 1678 | &config->desc.phys_base, |
| 1679 | GFP_KERNEL); |
| 1680 | if (config->desc.base == 0) { |
| 1681 | pr_err("tspp: error allocating sps descriptors"); |
| 1682 | rc = -ENOMEM; |
| 1683 | goto err_desc_alloc; |
| 1684 | } |
| 1685 | |
| 1686 | memset(config->desc.base, 0, config->desc.size); |
| 1687 | |
| 1688 | rc = sps_connect(channel->pipe, config); |
| 1689 | if (rc) { |
| 1690 | pr_err("tspp: error connecting bam"); |
| 1691 | goto err_connect; |
| 1692 | } |
| 1693 | |
| 1694 | event->mode = SPS_TRIGGER_CALLBACK; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1695 | event->options = SPS_O_DESC_DONE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1696 | event->callback = tspp_sps_complete_cb; |
| 1697 | event->xfer_done = NULL; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1698 | event->user = pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1699 | |
| 1700 | rc = sps_register_event(channel->pipe, event); |
| 1701 | if (rc) { |
| 1702 | pr_err("tspp: error registering event"); |
| 1703 | goto err_event; |
| 1704 | } |
| 1705 | |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1706 | init_timer(&channel->expiration_timer); |
| 1707 | channel->expiration_timer.function = tspp_expiration_timer; |
| 1708 | channel->expiration_timer.data = (unsigned long)pdev; |
| 1709 | channel->expiration_timer.expires = 0xffffffffL; |
| 1710 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1711 | rc = pm_runtime_get(&pdev->pdev->dev); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1712 | if (rc < 0) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1713 | dev_err(&pdev->pdev->dev, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1714 | "Runtime PM: Unable to wake up tspp device, rc = %d", |
| 1715 | rc); |
| 1716 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1717 | return 0; |
| 1718 | |
| 1719 | err_event: |
| 1720 | sps_disconnect(channel->pipe); |
| 1721 | err_connect: |
| 1722 | dma_free_coherent(NULL, config->desc.size, config->desc.base, |
| 1723 | config->desc.phys_base); |
| 1724 | err_desc_alloc: |
| 1725 | sps_free_endpoint(channel->pipe); |
| 1726 | err_sps_alloc: |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1727 | channel->used = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1728 | return rc; |
| 1729 | } |
| 1730 | EXPORT_SYMBOL(tspp_open_channel); |
| 1731 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1732 | /** |
| 1733 | * tspp_close_channel - close a TSPP channel. |
| 1734 | * |
| 1735 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 1736 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 1737 | * |
| 1738 | * Return error status |
| 1739 | * |
| 1740 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1741 | int tspp_close_channel(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1742 | { |
| 1743 | int i; |
| 1744 | int id; |
Liron Kuch | 4ed3bf6 | 2013-03-28 09:44:42 +0200 | [diff] [blame] | 1745 | int table_idx; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1746 | u32 val; |
Liron Kuch | e0acb41 | 2013-04-21 13:16:45 +0300 | [diff] [blame] | 1747 | unsigned long flags; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1748 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1749 | struct sps_connect *config; |
| 1750 | struct tspp_device *pdev; |
| 1751 | struct tspp_channel *channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1752 | |
| 1753 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1754 | pr_err("tspp: channel id out of range"); |
| 1755 | return -ECHRNG; |
| 1756 | } |
| 1757 | pdev = tspp_find_by_id(dev); |
| 1758 | if (!pdev) { |
| 1759 | pr_err("tspp_close: can't find device %i", dev); |
| 1760 | return -ENODEV; |
| 1761 | } |
| 1762 | channel = &pdev->channels[channel_id]; |
| 1763 | |
| 1764 | /* if the channel is not used, we are done */ |
| 1765 | if (!channel->used) |
| 1766 | return 0; |
| 1767 | |
Liron Kuch | e0acb41 | 2013-04-21 13:16:45 +0300 | [diff] [blame] | 1768 | /* |
| 1769 | * Need to protect access to used and waiting fields, as they are |
| 1770 | * used by the tasklet which is invoked from interrupt context |
| 1771 | */ |
| 1772 | spin_lock_irqsave(&pdev->spinlock, flags); |
| 1773 | channel->used = 0; |
| 1774 | channel->waiting = NULL; |
| 1775 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
| 1776 | |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1777 | if (channel->expiration_period_ms) |
| 1778 | del_timer(&channel->expiration_timer); |
| 1779 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1780 | channel->notifier = NULL; |
| 1781 | channel->notify_data = NULL; |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 1782 | channel->expiration_period_ms = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1783 | |
| 1784 | config = &channel->config; |
| 1785 | pdev = channel->pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1786 | |
| 1787 | /* disable pipe (channel) */ |
| 1788 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1789 | writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE); |
| 1790 | wmb(); |
| 1791 | |
| 1792 | /* unregister all filters for this channel */ |
Liron Kuch | 4ed3bf6 | 2013-03-28 09:44:42 +0200 | [diff] [blame] | 1793 | for (table_idx = 0; table_idx < TSPP_FILTER_TABLES; table_idx++) { |
| 1794 | for (i = 0; i < TSPP_NUM_PRIORITIES; i++) { |
| 1795 | struct tspp_pid_filter *filter = |
| 1796 | &pdev->filters[table_idx]->filter[i]; |
| 1797 | id = FILTER_GET_PIPE_NUMBER0(filter); |
| 1798 | if (id == channel->id) { |
| 1799 | if (FILTER_HAS_ENCRYPTION(filter)) |
| 1800 | tspp_free_key_entry( |
| 1801 | FILTER_GET_KEY_NUMBER(filter)); |
| 1802 | filter->config = 0; |
| 1803 | filter->filter = 0; |
| 1804 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | channel->filter_count = 0; |
| 1808 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1809 | /* disconnect the bam */ |
| 1810 | if (sps_disconnect(channel->pipe) != 0) |
| 1811 | pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id); |
| 1812 | |
| 1813 | /* destroy the buffers */ |
| 1814 | dma_free_coherent(NULL, config->desc.size, config->desc.base, |
| 1815 | config->desc.phys_base); |
| 1816 | |
Hamad Kadmany | 06ccf82 | 2013-12-12 14:10:20 +0200 | [diff] [blame] | 1817 | sps_free_endpoint(channel->pipe); |
| 1818 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1819 | tspp_destroy_buffers(channel_id, channel); |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 1820 | if (channel->dma_pool) { |
| 1821 | dma_pool_destroy(channel->dma_pool); |
| 1822 | channel->dma_pool = NULL; |
| 1823 | } |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1824 | |
| 1825 | channel->src = TSPP_SOURCE_NONE; |
| 1826 | channel->mode = TSPP_MODE_DISABLED; |
| 1827 | channel->memfree = NULL; |
| 1828 | channel->user_info = NULL; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1829 | channel->buffer_count = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1830 | channel->data = NULL; |
| 1831 | channel->read = NULL; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1832 | channel->locked = NULL; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1833 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 1834 | if (tspp_channels_in_use(pdev) == 0) { |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 1835 | sps_deregister_bam_device(pdev->bam_handle); |
| 1836 | pdev->bam_handle = SPS_DEV_HANDLE_INVALID; |
| 1837 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1838 | wake_unlock(&pdev->wake_lock); |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 1839 | tspp_clock_stop(pdev); |
| 1840 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1841 | |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 1842 | pm_runtime_put(&pdev->pdev->dev); |
| 1843 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1844 | return 0; |
| 1845 | } |
| 1846 | EXPORT_SYMBOL(tspp_close_channel); |
| 1847 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1848 | /** |
Hamad Kadmany | 6d2a9c7 | 2013-01-31 14:49:20 +0200 | [diff] [blame] | 1849 | * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter. |
| 1850 | * |
| 1851 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 1852 | * @source: The TSIF source from which the counter should be read |
| 1853 | * @tcr_counter: the value of TCR counter |
| 1854 | * |
| 1855 | * Return error status |
| 1856 | * |
| 1857 | * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz. |
| 1858 | * If source is neither TSIF 0 or TSIF1 0 is returned. |
| 1859 | */ |
| 1860 | int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter) |
| 1861 | { |
| 1862 | struct tspp_device *pdev; |
| 1863 | struct tspp_tsif_device *tsif_device; |
| 1864 | |
| 1865 | if (!tcr_counter) |
| 1866 | return -EINVAL; |
| 1867 | |
| 1868 | pdev = tspp_find_by_id(dev); |
| 1869 | if (!pdev) { |
| 1870 | pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev); |
| 1871 | return -ENODEV; |
| 1872 | } |
| 1873 | |
| 1874 | switch (source) { |
| 1875 | case TSPP_SOURCE_TSIF0: |
| 1876 | tsif_device = &pdev->tsif[0]; |
| 1877 | break; |
| 1878 | |
| 1879 | case TSPP_SOURCE_TSIF1: |
| 1880 | tsif_device = &pdev->tsif[1]; |
| 1881 | break; |
| 1882 | |
| 1883 | default: |
| 1884 | tsif_device = NULL; |
| 1885 | break; |
| 1886 | } |
| 1887 | |
| 1888 | if (tsif_device && tsif_device->ref_count) |
| 1889 | *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF); |
| 1890 | else |
| 1891 | *tcr_counter = 0; |
| 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | EXPORT_SYMBOL(tspp_get_ref_clk_counter); |
| 1896 | |
| 1897 | /** |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1898 | * tspp_add_filter - add a TSPP filter to a channel. |
| 1899 | * |
| 1900 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 1901 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 1902 | * @filter: TSPP filter parameters |
| 1903 | * |
| 1904 | * Return error status |
| 1905 | * |
| 1906 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1907 | int tspp_add_filter(u32 dev, u32 channel_id, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1908 | struct tspp_filter *filter) |
| 1909 | { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1910 | int i, rc; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1911 | int other_channel; |
| 1912 | int entry; |
| 1913 | u32 val, pid, enabled; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1914 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1915 | struct tspp_pid_filter p; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1916 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1917 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1918 | TSPP_DEBUG("tspp: add filter"); |
| 1919 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1920 | pr_err("tspp: channel id out of range"); |
| 1921 | return -ECHRNG; |
| 1922 | } |
| 1923 | pdev = tspp_find_by_id(dev); |
| 1924 | if (!pdev) { |
| 1925 | pr_err("tspp_add: can't find device %i", dev); |
| 1926 | return -ENODEV; |
| 1927 | } |
| 1928 | |
| 1929 | channel = &pdev->channels[channel_id]; |
| 1930 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1931 | if (filter->source > TSPP_SOURCE_MEM) { |
| 1932 | pr_err("tspp invalid source"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1933 | return -ENOSR; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | if (filter->priority >= TSPP_NUM_PRIORITIES) { |
Hamad Kadmany | 7d8a6f8 | 2013-11-17 11:31:01 +0200 | [diff] [blame] | 1937 | pr_err("tspp invalid filter priority"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1938 | return -ENOSR; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1939 | } |
| 1940 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 1941 | channel->mode = filter->mode; |
| 1942 | /* |
| 1943 | * if buffers are already allocated, verify they fulfil |
| 1944 | * the alignment requirements. |
| 1945 | */ |
| 1946 | if ((channel->buffer_count > 0) && |
| 1947 | (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode))) |
| 1948 | pr_warn("tspp: buffers allocated with incorrect alignment\n"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1949 | |
| 1950 | if (filter->mode == TSPP_MODE_PES) { |
| 1951 | for (i = 0; i < TSPP_NUM_PRIORITIES; i++) { |
| 1952 | struct tspp_pid_filter *tspp_filter = |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1953 | &pdev->filters[channel->src]->filter[i]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1954 | pid = FILTER_GET_PIPE_PID((tspp_filter)); |
| 1955 | enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter); |
| 1956 | if (enabled && (pid == filter->pid)) { |
| 1957 | other_channel = |
| 1958 | FILTER_GET_PIPE_NUMBER0(tspp_filter); |
| 1959 | pr_err("tspp: pid 0x%x already in use by channel %i", |
| 1960 | filter->pid, other_channel); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1961 | return -EBADSLT; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | /* make sure this priority is not already in use */ |
| 1967 | enabled = FILTER_GET_PIPE_PROCESS0( |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1968 | (&(pdev->filters[channel->src]->filter[filter->priority]))); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1969 | if (enabled) { |
| 1970 | pr_err("tspp: filter priority %i source %i is already enabled\n", |
| 1971 | filter->priority, channel->src); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1972 | return -ENOSR; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | if (channel->mode == TSPP_MODE_PES) { |
| 1976 | /* if we are already processing in PES mode, disable pipe |
| 1977 | (channel) and filter to be updated */ |
| 1978 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1979 | writel_relaxed(val | (1 << channel->id), |
| 1980 | pdev->base + TSPP_PS_DISABLE); |
| 1981 | wmb(); |
| 1982 | } |
| 1983 | |
| 1984 | /* update entry */ |
| 1985 | p.filter = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1986 | p.config = FILTER_TRANS_END_DISABLE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1987 | FILTER_SET_PIPE_PROCESS0((&p), filter->mode); |
| 1988 | FILTER_SET_PIPE_PID((&p), filter->pid); |
| 1989 | FILTER_SET_PID_MASK((&p), filter->mask); |
| 1990 | FILTER_SET_PIPE_NUMBER0((&p), channel->id); |
| 1991 | FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED); |
| 1992 | if (filter->decrypt) { |
| 1993 | entry = tspp_get_key_entry(); |
| 1994 | if (entry == -1) { |
| 1995 | pr_err("tspp: no more keys available!"); |
| 1996 | } else { |
| 1997 | p.config |= FILTER_DECRYPT; |
| 1998 | FILTER_SET_KEY_NUMBER((&p), entry); |
| 1999 | } |
| 2000 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2001 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2002 | pdev->filters[channel->src]-> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2003 | filter[filter->priority].config = p.config; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2004 | pdev->filters[channel->src]-> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2005 | filter[filter->priority].filter = p.filter; |
| 2006 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2007 | /* |
| 2008 | * allocate buffers if needed (i.e. if user did has not already called |
| 2009 | * tspp_allocate_buffers() explicitly). |
| 2010 | */ |
| 2011 | if (channel->buffer_count == 0) { |
| 2012 | channel->buffer_size = |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2013 | tspp_align_buffer_size_by_mode(channel->buffer_size, |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2014 | channel->mode); |
| 2015 | rc = tspp_allocate_buffers(dev, channel->id, |
| 2016 | channel->max_buffers, |
| 2017 | channel->buffer_size, |
| 2018 | channel->int_freq, NULL, NULL, NULL); |
| 2019 | if (rc != 0) { |
| 2020 | pr_err("tspp: tspp_allocate_buffers failed\n"); |
| 2021 | return rc; |
| 2022 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2025 | /* reenable pipe */ |
| 2026 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 2027 | writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE); |
| 2028 | wmb(); |
| 2029 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 2030 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2031 | channel->filter_count++; |
| 2032 | |
| 2033 | return 0; |
| 2034 | } |
| 2035 | EXPORT_SYMBOL(tspp_add_filter); |
| 2036 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2037 | /** |
| 2038 | * tspp_remove_filter - remove a TSPP filter from a channel. |
| 2039 | * |
| 2040 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2041 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2042 | * @filter: TSPP filter parameters |
| 2043 | * |
| 2044 | * Return error status |
| 2045 | * |
| 2046 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2047 | int tspp_remove_filter(u32 dev, u32 channel_id, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2048 | struct tspp_filter *filter) |
| 2049 | { |
| 2050 | int entry; |
| 2051 | u32 val; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2052 | struct tspp_device *pdev; |
| 2053 | int src; |
| 2054 | struct tspp_pid_filter *tspp_filter; |
| 2055 | struct tspp_channel *channel; |
| 2056 | |
| 2057 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 2058 | pr_err("tspp: channel id out of range"); |
| 2059 | return -ECHRNG; |
| 2060 | } |
| 2061 | pdev = tspp_find_by_id(dev); |
| 2062 | if (!pdev) { |
| 2063 | pr_err("tspp_remove: can't find device %i", dev); |
| 2064 | return -ENODEV; |
| 2065 | } |
Hamad Kadmany | 7d8a6f8 | 2013-11-17 11:31:01 +0200 | [diff] [blame] | 2066 | if (filter->priority >= TSPP_NUM_PRIORITIES) { |
| 2067 | pr_err("tspp invalid filter priority"); |
| 2068 | return -ENOSR; |
| 2069 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2070 | channel = &pdev->channels[channel_id]; |
| 2071 | |
| 2072 | src = channel->src; |
| 2073 | tspp_filter = &(pdev->filters[src]->filter[filter->priority]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2074 | |
| 2075 | /* disable pipe (channel) */ |
| 2076 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 2077 | writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE); |
| 2078 | wmb(); |
| 2079 | |
| 2080 | /* update data keys */ |
| 2081 | if (tspp_filter->config & FILTER_DECRYPT) { |
| 2082 | entry = FILTER_GET_KEY_NUMBER(tspp_filter); |
| 2083 | tspp_free_key_entry(entry); |
| 2084 | } |
| 2085 | |
| 2086 | /* update pid table */ |
| 2087 | tspp_filter->config = 0; |
| 2088 | tspp_filter->filter = 0; |
| 2089 | |
| 2090 | channel->filter_count--; |
| 2091 | |
| 2092 | /* reenable pipe */ |
| 2093 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 2094 | writel_relaxed(val & ~(1 << channel->id), |
| 2095 | pdev->base + TSPP_PS_DISABLE); |
| 2096 | wmb(); |
| 2097 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 2098 | |
| 2099 | return 0; |
| 2100 | } |
| 2101 | EXPORT_SYMBOL(tspp_remove_filter); |
| 2102 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2103 | /** |
| 2104 | * tspp_set_key - set TSPP key in key table. |
| 2105 | * |
| 2106 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2107 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2108 | * @key: TSPP key parameters |
| 2109 | * |
| 2110 | * Return error status |
| 2111 | * |
| 2112 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2113 | int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2114 | { |
| 2115 | int i; |
| 2116 | int id; |
| 2117 | int key_index; |
| 2118 | int data; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2119 | struct tspp_channel *channel; |
| 2120 | struct tspp_device *pdev; |
| 2121 | |
| 2122 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 2123 | pr_err("tspp: channel id out of range"); |
| 2124 | return -ECHRNG; |
| 2125 | } |
| 2126 | pdev = tspp_find_by_id(dev); |
| 2127 | if (!pdev) { |
| 2128 | pr_err("tspp_set: can't find device %i", dev); |
| 2129 | return -ENODEV; |
| 2130 | } |
| 2131 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2132 | |
| 2133 | /* read the key index used by this channel */ |
| 2134 | for (i = 0; i < TSPP_NUM_PRIORITIES; i++) { |
| 2135 | struct tspp_pid_filter *tspp_filter = |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2136 | &(pdev->filters[channel->src]->filter[i]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2137 | id = FILTER_GET_PIPE_NUMBER0(tspp_filter); |
| 2138 | if (id == channel->id) { |
| 2139 | if (FILTER_HAS_ENCRYPTION(tspp_filter)) { |
| 2140 | key_index = FILTER_GET_KEY_NUMBER(tspp_filter); |
| 2141 | break; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | if (i == TSPP_NUM_PRIORITIES) { |
| 2146 | pr_err("tspp: no encryption on this channel"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2147 | return -ENOKEY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | if (key->parity == TSPP_KEY_PARITY_EVEN) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2151 | pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb; |
| 2152 | pdev->tspp_key_table->entry[key_index].even_msb = key->msb; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2153 | } else { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2154 | pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb; |
| 2155 | pdev->tspp_key_table->entry[key_index].odd_msb = key->msb; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2156 | } |
| 2157 | data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID); |
| 2158 | |
| 2159 | return 0; |
| 2160 | } |
| 2161 | EXPORT_SYMBOL(tspp_set_key); |
| 2162 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2163 | /** |
| 2164 | * tspp_register_notification - register TSPP channel notification function. |
| 2165 | * |
| 2166 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2167 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2168 | * @pNotify: notification function |
| 2169 | * @userdata: user data to pass to notification function |
| 2170 | * @timer_ms: notification for partially filled buffers |
| 2171 | * |
| 2172 | * Return error status |
| 2173 | * |
| 2174 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2175 | int tspp_register_notification(u32 dev, u32 channel_id, |
| 2176 | tspp_notifier *pNotify, void *userdata, u32 timer_ms) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2177 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2178 | struct tspp_channel *channel; |
| 2179 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2180 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2181 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 2182 | pr_err("tspp: channel id out of range"); |
| 2183 | return -ECHRNG; |
| 2184 | } |
| 2185 | pdev = tspp_find_by_id(dev); |
| 2186 | if (!pdev) { |
| 2187 | pr_err("tspp_reg: can't find device %i", dev); |
| 2188 | return -ENODEV; |
| 2189 | } |
| 2190 | channel = &pdev->channels[channel_id]; |
| 2191 | channel->notifier = pNotify; |
| 2192 | channel->notify_data = userdata; |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 2193 | channel->expiration_period_ms = timer_ms; |
| 2194 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2195 | return 0; |
| 2196 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2197 | EXPORT_SYMBOL(tspp_register_notification); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2198 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2199 | /** |
| 2200 | * tspp_unregister_notification - unregister TSPP channel notification function. |
| 2201 | * |
| 2202 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2203 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2204 | * |
| 2205 | * Return error status |
| 2206 | * |
| 2207 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2208 | int tspp_unregister_notification(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2209 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2210 | struct tspp_channel *channel; |
| 2211 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2212 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2213 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 2214 | pr_err("tspp: channel id out of range"); |
| 2215 | return -ECHRNG; |
| 2216 | } |
| 2217 | pdev = tspp_find_by_id(dev); |
| 2218 | if (!pdev) { |
| 2219 | pr_err("tspp_unreg: can't find device %i", dev); |
| 2220 | return -ENODEV; |
| 2221 | } |
| 2222 | channel = &pdev->channels[channel_id]; |
| 2223 | channel->notifier = NULL; |
| 2224 | channel->notify_data = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2225 | return 0; |
| 2226 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2227 | EXPORT_SYMBOL(tspp_unregister_notification); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2228 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2229 | /** |
| 2230 | * tspp_get_buffer - get TSPP data buffer. |
| 2231 | * |
| 2232 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2233 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2234 | * |
| 2235 | * Return error status |
| 2236 | * |
| 2237 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2238 | const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2239 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2240 | struct tspp_mem_buffer *buffer; |
| 2241 | struct tspp_channel *channel; |
| 2242 | struct tspp_device *pdev; |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2243 | unsigned long flags; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2244 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2245 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 2246 | pr_err("tspp: channel id out of range"); |
| 2247 | return NULL; |
| 2248 | } |
| 2249 | pdev = tspp_find_by_id(dev); |
| 2250 | if (!pdev) { |
| 2251 | pr_err("tspp_get: can't find device %i", dev); |
| 2252 | return NULL; |
| 2253 | } |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2254 | |
| 2255 | spin_lock_irqsave(&pdev->spinlock, flags); |
| 2256 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2257 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2258 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2259 | if (!channel->read) { |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2260 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2261 | pr_warn("tspp: no buffer to get on channel %i!", |
| 2262 | channel->id); |
| 2263 | return NULL; |
| 2264 | } |
| 2265 | |
| 2266 | buffer = channel->read; |
| 2267 | /* see if we have any buffers ready to read */ |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2268 | if (buffer->state != TSPP_BUF_STATE_DATA) { |
| 2269 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
| 2270 | return NULL; |
| 2271 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2272 | |
| 2273 | if (buffer->state == TSPP_BUF_STATE_DATA) { |
| 2274 | /* mark the buffer as busy */ |
| 2275 | buffer->state = TSPP_BUF_STATE_LOCKED; |
| 2276 | |
| 2277 | /* increment the pointer along the list */ |
| 2278 | channel->read = channel->read->next; |
| 2279 | } |
| 2280 | |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2281 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
| 2282 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2283 | return &buffer->desc; |
| 2284 | } |
| 2285 | EXPORT_SYMBOL(tspp_get_buffer); |
| 2286 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2287 | /** |
| 2288 | * tspp_release_buffer - release TSPP data buffer back to TSPP. |
| 2289 | * |
| 2290 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2291 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2292 | * @descriptor_id: buffer descriptor ID |
| 2293 | * |
| 2294 | * Return error status |
| 2295 | * |
| 2296 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2297 | int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id) |
| 2298 | { |
| 2299 | int i, found = 0; |
| 2300 | struct tspp_mem_buffer *buffer; |
| 2301 | struct tspp_channel *channel; |
| 2302 | struct tspp_device *pdev; |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2303 | unsigned long flags; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2304 | |
| 2305 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 2306 | pr_err("tspp: channel id out of range"); |
| 2307 | return -ECHRNG; |
| 2308 | } |
| 2309 | pdev = tspp_find_by_id(dev); |
| 2310 | if (!pdev) { |
| 2311 | pr_err("tspp: can't find device %i", dev); |
| 2312 | return -ENODEV; |
| 2313 | } |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2314 | |
| 2315 | spin_lock_irqsave(&pdev->spinlock, flags); |
| 2316 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2317 | channel = &pdev->channels[channel_id]; |
| 2318 | |
| 2319 | if (descriptor_id > channel->buffer_count) |
| 2320 | pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id); |
| 2321 | |
| 2322 | /* find the correct descriptor */ |
| 2323 | buffer = channel->locked; |
| 2324 | for (i = 0; i < channel->buffer_count; i++) { |
| 2325 | if (buffer->desc.id == descriptor_id) { |
| 2326 | found = 1; |
| 2327 | break; |
| 2328 | } |
| 2329 | buffer = buffer->next; |
| 2330 | } |
| 2331 | channel->locked = channel->locked->next; |
| 2332 | |
| 2333 | if (!found) { |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2334 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2335 | pr_err("tspp: cant find desc %i", descriptor_id); |
| 2336 | return -EINVAL; |
| 2337 | } |
| 2338 | |
| 2339 | /* make sure the buffer is in the expected state */ |
| 2340 | if (buffer->state != TSPP_BUF_STATE_LOCKED) { |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2341 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2342 | pr_err("tspp: buffer %i not locked", descriptor_id); |
| 2343 | return -EINVAL; |
| 2344 | } |
| 2345 | /* unlock the buffer and requeue it */ |
| 2346 | buffer->state = TSPP_BUF_STATE_WAITING; |
| 2347 | |
| 2348 | if (tspp_queue_buffer(channel, buffer)) |
| 2349 | pr_warn("tspp: can't requeue buffer"); |
Gilad Broner | fe77c22 | 2014-06-16 11:03:51 +0300 | [diff] [blame] | 2350 | |
| 2351 | spin_unlock_irqrestore(&pdev->spinlock, flags); |
| 2352 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2353 | return 0; |
| 2354 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2355 | EXPORT_SYMBOL(tspp_release_buffer); |
| 2356 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2357 | /** |
| 2358 | * tspp_allocate_buffers - allocate TSPP data buffers. |
| 2359 | * |
| 2360 | * @dev: TSPP device (up to TSPP_MAX_DEVICES) |
| 2361 | * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS) |
| 2362 | * @count: number of buffers to allocate |
| 2363 | * @size: size of each buffer to allocate |
| 2364 | * @int_freq: interrupt frequency |
| 2365 | * @alloc: user defined memory allocator function. Pass NULL for default. |
| 2366 | * @memfree: user defined memory free function. Pass NULL for default. |
| 2367 | * @user: user data to pass to the memory allocator/free function |
| 2368 | * |
| 2369 | * Return error status |
| 2370 | * |
| 2371 | * The user can optionally call this function explicitly to allocate the TSPP |
| 2372 | * data buffers. Alternatively, if the user did not call this function, it |
| 2373 | * is called implicitly by tspp_add_filter(). |
| 2374 | */ |
| 2375 | int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size, |
| 2376 | u32 int_freq, tspp_allocator *alloc, |
| 2377 | tspp_memfree *memfree, void *user) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2378 | { |
| 2379 | struct tspp_channel *channel; |
| 2380 | struct tspp_device *pdev; |
| 2381 | struct tspp_mem_buffer *last = NULL; |
| 2382 | |
| 2383 | TSPP_DEBUG("tspp_allocate_buffers"); |
| 2384 | |
| 2385 | if (channel_id >= TSPP_NUM_CHANNELS) { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2386 | pr_err("%s: channel id out of range", __func__); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2387 | return -ECHRNG; |
| 2388 | } |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2389 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2390 | pdev = tspp_find_by_id(dev); |
| 2391 | if (!pdev) { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2392 | pr_err("%s: can't find device %i", __func__, dev); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2393 | return -ENODEV; |
| 2394 | } |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2395 | |
| 2396 | if (count < MIN_ACCEPTABLE_BUFFER_COUNT) { |
| 2397 | pr_err("%s: tspp requires a minimum of %i buffers\n", |
| 2398 | __func__, MIN_ACCEPTABLE_BUFFER_COUNT); |
| 2399 | return -EINVAL; |
| 2400 | } |
| 2401 | |
Hamad Kadmany | 2402035 | 2013-05-22 12:54:18 +0300 | [diff] [blame] | 2402 | if (count > TSPP_NUM_BUFFERS) { |
| 2403 | pr_err("%s: tspp requires a maximum of %i buffers\n", |
| 2404 | __func__, TSPP_NUM_BUFFERS); |
| 2405 | return -EINVAL; |
| 2406 | } |
| 2407 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2408 | channel = &pdev->channels[channel_id]; |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2409 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2410 | /* allow buffer allocation only if there was no previous buffer |
| 2411 | * allocation for this channel. |
| 2412 | */ |
| 2413 | if (channel->buffer_count > 0) { |
| 2414 | pr_err("%s: buffers already allocated for channel %u", |
| 2415 | __func__, channel_id); |
| 2416 | return -EINVAL; |
| 2417 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2418 | |
| 2419 | channel->max_buffers = count; |
| 2420 | |
| 2421 | /* set up interrupt frequency */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2422 | if (int_freq > channel->max_buffers) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2423 | int_freq = channel->max_buffers; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2424 | pr_warn("%s: setting interrupt frequency to %u\n", |
| 2425 | __func__, int_freq); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2426 | } |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2427 | channel->int_freq = int_freq; |
| 2428 | /* |
| 2429 | * it is the responsibility of the caller to tspp_allocate_buffers(), |
| 2430 | * whether it's the user or the driver, to make sure the size parameter |
| 2431 | * is compatible to the channel mode. |
| 2432 | */ |
| 2433 | channel->buffer_size = size; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2434 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2435 | /* save user defined memory free function for later use */ |
| 2436 | channel->memfree = memfree; |
| 2437 | channel->user_info = user; |
| 2438 | |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2439 | /* |
| 2440 | * For small buffers, create a DMA pool so that memory |
| 2441 | * is not wasted through dma_alloc_coherent. |
| 2442 | */ |
| 2443 | if (TSPP_USE_DMA_POOL(channel->buffer_size)) { |
| 2444 | channel->dma_pool = dma_pool_create("tspp", |
| 2445 | NULL, channel->buffer_size, 0, 0); |
| 2446 | if (!channel->dma_pool) { |
| 2447 | pr_err("%s: Can't allocate memory pool\n", __func__); |
| 2448 | return -ENOMEM; |
| 2449 | } |
| 2450 | } else { |
| 2451 | channel->dma_pool = NULL; |
| 2452 | } |
| 2453 | |
| 2454 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2455 | for (channel->buffer_count = 0; |
| 2456 | channel->buffer_count < channel->max_buffers; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2457 | channel->buffer_count++) { |
| 2458 | |
| 2459 | /* allocate the descriptor */ |
| 2460 | struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *) |
| 2461 | kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL); |
| 2462 | if (!desc) { |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2463 | pr_warn("%s: Can't allocate desc %i", |
| 2464 | __func__, channel->buffer_count); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2465 | break; |
| 2466 | } |
| 2467 | |
| 2468 | desc->desc.id = channel->buffer_count; |
| 2469 | /* allocate the buffer */ |
| 2470 | if (tspp_alloc_buffer(channel_id, &desc->desc, |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2471 | channel->buffer_size, channel->dma_pool, |
| 2472 | alloc, user) != 0) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2473 | kfree(desc); |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2474 | pr_warn("%s: Can't allocate buffer %i", |
| 2475 | __func__, channel->buffer_count); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2476 | break; |
| 2477 | } |
| 2478 | |
| 2479 | /* add the descriptor to the list */ |
| 2480 | desc->filled = 0; |
| 2481 | desc->read_index = 0; |
| 2482 | if (!channel->data) { |
| 2483 | channel->data = desc; |
| 2484 | desc->next = channel->data; |
| 2485 | } else { |
| 2486 | last->next = desc; |
| 2487 | } |
| 2488 | last = desc; |
| 2489 | desc->next = channel->data; |
| 2490 | |
| 2491 | /* prepare the sps descriptor */ |
| 2492 | desc->sps.phys_base = desc->desc.phys_base; |
| 2493 | desc->sps.base = desc->desc.virt_base; |
| 2494 | desc->sps.size = desc->desc.size; |
| 2495 | |
| 2496 | /* start the transfer */ |
| 2497 | if (tspp_queue_buffer(channel, desc)) |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2498 | pr_err("%s: can't queue buffer %i", |
| 2499 | __func__, desc->desc.id); |
| 2500 | } |
| 2501 | |
| 2502 | if (channel->buffer_count < channel->max_buffers) { |
| 2503 | /* |
| 2504 | * we failed to allocate the requested number of buffers. |
| 2505 | * we don't allow a partial success, so need to clean up here. |
| 2506 | */ |
| 2507 | tspp_destroy_buffers(channel_id, channel); |
| 2508 | channel->buffer_count = 0; |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2509 | |
| 2510 | if (channel->dma_pool) { |
| 2511 | dma_pool_destroy(channel->dma_pool); |
| 2512 | channel->dma_pool = NULL; |
| 2513 | } |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2514 | return -ENOMEM; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | channel->waiting = channel->data; |
| 2518 | channel->read = channel->data; |
| 2519 | channel->locked = channel->data; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2520 | |
Hamad Kadmany | 81cee05 | 2012-11-29 14:15:57 +0200 | [diff] [blame] | 2521 | /* Now that buffers are scheduled to HW, kick data expiration timer */ |
| 2522 | if (channel->expiration_period_ms) |
| 2523 | mod_timer(&channel->expiration_timer, |
| 2524 | jiffies + |
| 2525 | MSEC_TO_JIFFIES( |
| 2526 | channel->expiration_period_ms)); |
| 2527 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2528 | return 0; |
| 2529 | } |
| 2530 | EXPORT_SYMBOL(tspp_allocate_buffers); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2531 | |
| 2532 | /*** File Operations ***/ |
| 2533 | static ssize_t tspp_open(struct inode *inode, struct file *filp) |
| 2534 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2535 | u32 dev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2536 | struct tspp_channel *channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2537 | |
| 2538 | TSPP_DEBUG("tspp_open"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2539 | channel = container_of(inode->i_cdev, struct tspp_channel, cdev); |
| 2540 | filp->private_data = channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2541 | dev = channel->pdev->pdev->id; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2542 | |
| 2543 | /* if this channel is already in use, quit */ |
| 2544 | if (channel->used) { |
| 2545 | pr_err("tspp channel %i already in use", |
| 2546 | MINOR(channel->cdev.dev)); |
| 2547 | return -EACCES; |
| 2548 | } |
| 2549 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2550 | if (tspp_open_channel(dev, channel->id) != 0) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2551 | pr_err("tspp: error opening channel"); |
| 2552 | return -EACCES; |
| 2553 | } |
| 2554 | |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
| 2558 | static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p) |
| 2559 | { |
| 2560 | unsigned long flags; |
| 2561 | unsigned int mask = 0; |
| 2562 | struct tspp_channel *channel; |
| 2563 | channel = filp->private_data; |
| 2564 | |
| 2565 | /* register the wait queue for this channel */ |
| 2566 | poll_wait(filp, &channel->in_queue, p); |
| 2567 | |
| 2568 | spin_lock_irqsave(&channel->pdev->spinlock, flags); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2569 | if (channel->read && |
| 2570 | channel->read->state == TSPP_BUF_STATE_DATA) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2571 | mask = POLLIN | POLLRDNORM; |
| 2572 | |
| 2573 | spin_unlock_irqrestore(&channel->pdev->spinlock, flags); |
| 2574 | |
| 2575 | return mask; |
| 2576 | } |
| 2577 | |
| 2578 | static ssize_t tspp_release(struct inode *inode, struct file *filp) |
| 2579 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2580 | struct tspp_channel *channel = filp->private_data; |
| 2581 | u32 dev = channel->pdev->pdev->id; |
| 2582 | TSPP_DEBUG("tspp_release"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2583 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2584 | tspp_close_channel(dev, channel->id); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2585 | |
| 2586 | return 0; |
| 2587 | } |
| 2588 | |
| 2589 | static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count, |
| 2590 | loff_t *f_pos) |
| 2591 | { |
| 2592 | size_t size = 0; |
| 2593 | size_t transferred = 0; |
| 2594 | struct tspp_channel *channel; |
| 2595 | struct tspp_mem_buffer *buffer; |
| 2596 | channel = filp->private_data; |
| 2597 | |
| 2598 | TSPP_DEBUG("tspp_read"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2599 | |
| 2600 | while (!channel->read) { |
| 2601 | if (filp->f_flags & O_NONBLOCK) { |
| 2602 | pr_warn("tspp: no buffer on channel %i!", |
| 2603 | channel->id); |
| 2604 | return -EAGAIN; |
| 2605 | } |
| 2606 | /* go to sleep if there is nothing to read */ |
| 2607 | if (wait_event_interruptible(channel->in_queue, |
| 2608 | (channel->read != NULL))) { |
| 2609 | pr_err("tspp: rude awakening\n"); |
| 2610 | return -ERESTARTSYS; |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | buffer = channel->read; |
| 2615 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2616 | /* see if we have any buffers ready to read */ |
| 2617 | while (buffer->state != TSPP_BUF_STATE_DATA) { |
| 2618 | if (filp->f_flags & O_NONBLOCK) { |
| 2619 | pr_warn("tspp: nothing to read on channel %i!", |
| 2620 | channel->id); |
| 2621 | return -EAGAIN; |
| 2622 | } |
| 2623 | /* go to sleep if there is nothing to read */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2624 | if (wait_event_interruptible(channel->in_queue, |
| 2625 | (buffer->state == TSPP_BUF_STATE_DATA))) { |
| 2626 | pr_err("tspp: rude awakening\n"); |
| 2627 | return -ERESTARTSYS; |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | while (buffer->state == TSPP_BUF_STATE_DATA) { |
| 2632 | size = min(count, buffer->filled); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2633 | if (size == 0) |
| 2634 | break; |
| 2635 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2636 | if (copy_to_user(buf, buffer->desc.virt_base + |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2637 | buffer->read_index, size)) { |
| 2638 | pr_err("tspp: error copying to user buffer"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2639 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2640 | } |
| 2641 | buf += size; |
| 2642 | count -= size; |
| 2643 | transferred += size; |
| 2644 | buffer->read_index += size; |
| 2645 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2646 | /* |
| 2647 | * after reading the end of the buffer, requeue it, |
| 2648 | * and set up for reading the next one |
| 2649 | */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2650 | if (buffer->read_index == buffer->filled) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2651 | buffer->state = TSPP_BUF_STATE_WAITING; |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2652 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2653 | if (tspp_queue_buffer(channel, buffer)) |
| 2654 | pr_err("tspp: can't submit transfer"); |
Hamad Kadmany | 090709b | 2013-01-06 12:08:13 +0200 | [diff] [blame] | 2655 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2656 | channel->locked = channel->read; |
| 2657 | channel->read = channel->read->next; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | return transferred; |
| 2662 | } |
| 2663 | |
| 2664 | static long tspp_ioctl(struct file *filp, |
| 2665 | unsigned int param0, unsigned long param1) |
| 2666 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2667 | u32 dev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2668 | int rc = -1; |
| 2669 | struct tspp_channel *channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2670 | struct tspp_select_source ss; |
| 2671 | struct tspp_filter f; |
| 2672 | struct tspp_key k; |
| 2673 | struct tspp_iv iv; |
| 2674 | struct tspp_system_keys sk; |
| 2675 | struct tspp_buffer b; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2676 | channel = filp->private_data; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2677 | dev = channel->pdev->pdev->id; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2678 | |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 2679 | if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2680 | return -EINVAL; |
| 2681 | |
| 2682 | switch (param0) { |
| 2683 | case TSPP_IOCTL_SELECT_SOURCE: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2684 | if (!access_ok(VERIFY_READ, param1, |
| 2685 | sizeof(struct tspp_select_source))) { |
| 2686 | return -EBUSY; |
| 2687 | } |
| 2688 | if (__copy_from_user(&ss, (void *)param1, |
| 2689 | sizeof(struct tspp_select_source)) == 0) |
| 2690 | rc = tspp_select_source(dev, channel->id, &ss); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2691 | break; |
| 2692 | case TSPP_IOCTL_ADD_FILTER: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2693 | if (!access_ok(VERIFY_READ, param1, |
| 2694 | sizeof(struct tspp_filter))) { |
| 2695 | return -ENOSR; |
| 2696 | } |
| 2697 | if (__copy_from_user(&f, (void *)param1, |
| 2698 | sizeof(struct tspp_filter)) == 0) |
| 2699 | rc = tspp_add_filter(dev, channel->id, &f); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2700 | break; |
| 2701 | case TSPP_IOCTL_REMOVE_FILTER: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2702 | if (!access_ok(VERIFY_READ, param1, |
| 2703 | sizeof(struct tspp_filter))) { |
| 2704 | return -EBUSY; |
| 2705 | } |
| 2706 | if (__copy_from_user(&f, (void *)param1, |
| 2707 | sizeof(struct tspp_filter)) == 0) |
| 2708 | rc = tspp_remove_filter(dev, channel->id, &f); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2709 | break; |
| 2710 | case TSPP_IOCTL_SET_KEY: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2711 | if (!access_ok(VERIFY_READ, param1, |
| 2712 | sizeof(struct tspp_key))) { |
| 2713 | return -EBUSY; |
| 2714 | } |
| 2715 | if (__copy_from_user(&k, (void *)param1, |
| 2716 | sizeof(struct tspp_key)) == 0) |
| 2717 | rc = tspp_set_key(dev, channel->id, &k); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2718 | break; |
| 2719 | case TSPP_IOCTL_SET_IV: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2720 | if (!access_ok(VERIFY_READ, param1, |
| 2721 | sizeof(struct tspp_iv))) { |
| 2722 | return -EBUSY; |
| 2723 | } |
| 2724 | if (__copy_from_user(&iv, (void *)param1, |
| 2725 | sizeof(struct tspp_iv)) == 0) |
| 2726 | rc = tspp_set_iv(channel, &iv); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2727 | break; |
| 2728 | case TSPP_IOCTL_SET_SYSTEM_KEYS: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2729 | if (!access_ok(VERIFY_READ, param1, |
| 2730 | sizeof(struct tspp_system_keys))) { |
| 2731 | return -EINVAL; |
| 2732 | } |
| 2733 | if (__copy_from_user(&sk, (void *)param1, |
| 2734 | sizeof(struct tspp_system_keys)) == 0) |
| 2735 | rc = tspp_set_system_keys(channel, &sk); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2736 | break; |
| 2737 | case TSPP_IOCTL_BUFFER_SIZE: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2738 | if (!access_ok(VERIFY_READ, param1, |
| 2739 | sizeof(struct tspp_buffer))) { |
| 2740 | rc = -EINVAL; |
| 2741 | } |
| 2742 | if (__copy_from_user(&b, (void *)param1, |
| 2743 | sizeof(struct tspp_buffer)) == 0) |
| 2744 | rc = tspp_set_buffer_size(channel, &b); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2745 | break; |
Liron Kuch | a7b49ae | 2013-02-14 16:26:38 +0200 | [diff] [blame] | 2746 | case TSPP_IOCTL_CLOSE_STREAM: |
| 2747 | rc = tspp_close_stream(dev, channel->id); |
| 2748 | break; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2749 | default: |
| 2750 | pr_err("tspp: Unknown ioctl %i", param0); |
| 2751 | } |
| 2752 | |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2753 | /* |
| 2754 | * normalize the return code in case one of the subfunctions does |
| 2755 | * something weird |
| 2756 | */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2757 | if (rc != 0) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2758 | rc = -ENOIOCTLCMD; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2759 | |
| 2760 | return rc; |
| 2761 | } |
| 2762 | |
| 2763 | /*** debugfs ***/ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2764 | static int debugfs_iomem_x32_set(void *data, u64 val) |
| 2765 | { |
| 2766 | writel_relaxed(val, data); |
| 2767 | wmb(); |
| 2768 | return 0; |
| 2769 | } |
| 2770 | |
| 2771 | static int debugfs_iomem_x32_get(void *data, u64 *val) |
| 2772 | { |
| 2773 | *val = readl_relaxed(data); |
| 2774 | return 0; |
| 2775 | } |
| 2776 | |
| 2777 | DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get, |
| 2778 | debugfs_iomem_x32_set, "0x%08llx"); |
| 2779 | |
| 2780 | static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device, |
| 2781 | int instance) |
| 2782 | { |
| 2783 | char name[10]; |
| 2784 | snprintf(name, 10, "tsif%i", instance); |
| 2785 | tsif_device->dent_tsif = debugfs_create_dir( |
| 2786 | name, NULL); |
| 2787 | if (tsif_device->dent_tsif) { |
| 2788 | int i; |
| 2789 | void __iomem *base = tsif_device->base; |
| 2790 | for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) { |
| 2791 | tsif_device->debugfs_tsif_regs[i] = |
| 2792 | debugfs_create_file( |
| 2793 | debugfs_tsif_regs[i].name, |
| 2794 | debugfs_tsif_regs[i].mode, |
| 2795 | tsif_device->dent_tsif, |
| 2796 | base + debugfs_tsif_regs[i].offset, |
| 2797 | &fops_iomem_x32); |
| 2798 | } |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 2799 | |
| 2800 | debugfs_create_u32( |
| 2801 | "stat_rx_chunks", |
Hamad Kadmany | a1dde82 | 2013-03-28 08:23:26 +0200 | [diff] [blame] | 2802 | S_IRUGO | S_IWUSR | S_IWGRP, |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 2803 | tsif_device->dent_tsif, |
| 2804 | &tsif_device->stat_rx); |
| 2805 | |
| 2806 | debugfs_create_u32( |
| 2807 | "stat_overflow", |
Hamad Kadmany | a1dde82 | 2013-03-28 08:23:26 +0200 | [diff] [blame] | 2808 | S_IRUGO | S_IWUSR | S_IWGRP, |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 2809 | tsif_device->dent_tsif, |
| 2810 | &tsif_device->stat_overflow); |
| 2811 | |
| 2812 | debugfs_create_u32( |
| 2813 | "stat_lost_sync", |
Hamad Kadmany | a1dde82 | 2013-03-28 08:23:26 +0200 | [diff] [blame] | 2814 | S_IRUGO | S_IWUSR | S_IWGRP, |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 2815 | tsif_device->dent_tsif, |
| 2816 | &tsif_device->stat_lost_sync); |
| 2817 | |
| 2818 | debugfs_create_u32( |
| 2819 | "stat_timeout", |
Hamad Kadmany | a1dde82 | 2013-03-28 08:23:26 +0200 | [diff] [blame] | 2820 | S_IRUGO | S_IWUSR | S_IWGRP, |
Hamad Kadmany | 44307d3 | 2012-11-25 09:49:51 +0200 | [diff] [blame] | 2821 | tsif_device->dent_tsif, |
| 2822 | &tsif_device->stat_timeout); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device) |
| 2827 | { |
| 2828 | if (tsif_device->dent_tsif) { |
| 2829 | int i; |
| 2830 | debugfs_remove_recursive(tsif_device->dent_tsif); |
| 2831 | tsif_device->dent_tsif = NULL; |
| 2832 | for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) |
| 2833 | tsif_device->debugfs_tsif_regs[i] = NULL; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | static void tspp_debugfs_init(struct tspp_device *device, int instance) |
| 2838 | { |
| 2839 | char name[10]; |
| 2840 | snprintf(name, 10, "tspp%i", instance); |
| 2841 | device->dent = debugfs_create_dir( |
| 2842 | name, NULL); |
| 2843 | if (device->dent) { |
| 2844 | int i; |
| 2845 | void __iomem *base = device->base; |
| 2846 | for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) { |
| 2847 | device->debugfs_regs[i] = |
| 2848 | debugfs_create_file( |
| 2849 | debugfs_tspp_regs[i].name, |
| 2850 | debugfs_tspp_regs[i].mode, |
| 2851 | device->dent, |
| 2852 | base + debugfs_tspp_regs[i].offset, |
| 2853 | &fops_iomem_x32); |
| 2854 | } |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | static void tspp_debugfs_exit(struct tspp_device *device) |
| 2859 | { |
| 2860 | if (device->dent) { |
| 2861 | int i; |
| 2862 | debugfs_remove_recursive(device->dent); |
| 2863 | device->dent = NULL; |
| 2864 | for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) |
| 2865 | device->debugfs_regs[i] = NULL; |
| 2866 | } |
| 2867 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2868 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2869 | /* copy device-tree data to platfrom data struct */ |
| 2870 | static __devinit struct msm_tspp_platform_data * |
| 2871 | msm_tspp_dt_to_pdata(struct platform_device *pdev) |
| 2872 | { |
| 2873 | struct device_node *node = pdev->dev.of_node; |
| 2874 | struct msm_tspp_platform_data *data; |
| 2875 | struct msm_gpio *gpios; |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 2876 | struct property *prop; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2877 | int i, rc; |
| 2878 | int gpio; |
| 2879 | u32 gpio_func; |
| 2880 | |
| 2881 | /* Note: memory allocated by devm_kzalloc is freed automatically */ |
| 2882 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 2883 | if (!data) { |
| 2884 | pr_err("tspp: Unable to allocate platform data\n"); |
| 2885 | return NULL; |
| 2886 | } |
| 2887 | rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk); |
| 2888 | if (rc) { |
| 2889 | pr_err("tspp: Could not find tsif-pclk property, err = %d\n", |
| 2890 | rc); |
| 2891 | return NULL; |
| 2892 | } |
| 2893 | rc = of_property_read_string(node, "qcom,tsif-ref-clk", |
| 2894 | &data->tsif_ref_clk); |
| 2895 | if (rc) { |
| 2896 | pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n", |
| 2897 | rc); |
| 2898 | return NULL; |
| 2899 | } |
| 2900 | |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 2901 | data->tsif_vreg_present = 0; |
| 2902 | prop = of_find_property(node, "vdd_cx-supply", NULL); |
| 2903 | if (prop) |
| 2904 | data->tsif_vreg_present = 1; |
| 2905 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2906 | data->num_gpios = of_gpio_count(node); |
| 2907 | if (data->num_gpios == 0) { |
| 2908 | pr_err("tspp: Could not find GPIO definitions\n"); |
| 2909 | return NULL; |
| 2910 | } |
| 2911 | gpios = devm_kzalloc(&pdev->dev, |
| 2912 | (data->num_gpios * sizeof(struct msm_gpio)), |
| 2913 | GFP_KERNEL); |
| 2914 | if (!gpios) { |
| 2915 | pr_err("tspp: Unable to allocate memory for GPIOs table\n"); |
| 2916 | return NULL; |
| 2917 | } |
| 2918 | /* Assuming GPIO FUNC property is the same for all GPIOs */ |
| 2919 | if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) { |
| 2920 | pr_err("tspp: Could not find gpios-func property\n"); |
| 2921 | return NULL; |
| 2922 | } |
| 2923 | for (i = 0; i < data->num_gpios; i++) { |
| 2924 | gpio = of_get_gpio(node, i); |
| 2925 | gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func, |
| 2926 | GPIO_CFG_INPUT, |
| 2927 | GPIO_CFG_PULL_DOWN, |
| 2928 | GPIO_CFG_2MA); |
| 2929 | rc = of_property_read_string_index(node, "qcom,gpio-names", |
| 2930 | i, &gpios[i].label); |
| 2931 | if (rc) |
| 2932 | pr_warn("tspp: Could not find gpio-names property\n"); |
| 2933 | } |
| 2934 | |
| 2935 | data->gpios = gpios; |
| 2936 | |
| 2937 | return data; |
| 2938 | } |
| 2939 | |
| 2940 | static int msm_tspp_map_irqs(struct platform_device *pdev, |
| 2941 | struct tspp_device *device) |
| 2942 | { |
| 2943 | int rc; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* get IRQ numbers from platform information */ |
| 2946 | |
| 2947 | /* map TSPP IRQ */ |
| 2948 | rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ"); |
| 2949 | if (rc > 0) { |
| 2950 | device->tspp_irq = rc; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2951 | } else { |
| 2952 | dev_err(&pdev->dev, "failed to get TSPP IRQ"); |
| 2953 | return -EINVAL; |
| 2954 | } |
| 2955 | |
| 2956 | /* map TSIF IRQs */ |
| 2957 | rc = platform_get_irq_byname(pdev, "TSIF0_IRQ"); |
| 2958 | if (rc > 0) { |
| 2959 | device->tsif[0].tsif_irq = rc; |
| 2960 | } else { |
| 2961 | dev_err(&pdev->dev, "failed to get TSIF0 IRQ"); |
| 2962 | return -EINVAL; |
| 2963 | } |
| 2964 | |
| 2965 | rc = platform_get_irq_byname(pdev, "TSIF1_IRQ"); |
| 2966 | if (rc > 0) { |
| 2967 | device->tsif[1].tsif_irq = rc; |
| 2968 | } else { |
| 2969 | dev_err(&pdev->dev, "failed to get TSIF1 IRQ"); |
| 2970 | return -EINVAL; |
| 2971 | } |
| 2972 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2973 | /* map BAM IRQ */ |
| 2974 | rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ"); |
| 2975 | if (rc > 0) { |
| 2976 | device->bam_irq = rc; |
| 2977 | } else { |
| 2978 | dev_err(&pdev->dev, "failed to get TSPP BAM IRQ"); |
| 2979 | return -EINVAL; |
| 2980 | } |
| 2981 | |
| 2982 | return 0; |
| 2983 | } |
| 2984 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2985 | static int __devinit msm_tspp_probe(struct platform_device *pdev) |
| 2986 | { |
| 2987 | int rc = -ENODEV; |
| 2988 | u32 version; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2989 | u32 i, j; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2990 | struct msm_tspp_platform_data *data; |
| 2991 | struct tspp_device *device; |
| 2992 | struct resource *mem_tsif0; |
| 2993 | struct resource *mem_tsif1; |
| 2994 | struct resource *mem_tspp; |
| 2995 | struct resource *mem_bam; |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 2996 | struct tspp_channel *channel; |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 2997 | struct msm_bus_scale_pdata *tspp_bus_pdata = NULL; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2998 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 2999 | if (pdev->dev.of_node) { |
| 3000 | /* get information from device tree */ |
| 3001 | data = msm_tspp_dt_to_pdata(pdev); |
| 3002 | /* get device ID */ |
| 3003 | rc = of_property_read_u32(pdev->dev.of_node, |
| 3004 | "cell-index", &pdev->id); |
| 3005 | if (rc) |
| 3006 | pdev->id = -1; |
| 3007 | |
| 3008 | pdev->dev.platform_data = data; |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 3009 | |
| 3010 | tspp_bus_pdata = msm_bus_cl_get_pdata(pdev); |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3011 | } else { |
| 3012 | /* must have platform data */ |
| 3013 | data = pdev->dev.platform_data; |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 3014 | tspp_bus_pdata = NULL; |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3015 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3016 | if (!data) { |
| 3017 | pr_err("tspp: Platform data not available"); |
| 3018 | rc = -EINVAL; |
| 3019 | goto out; |
| 3020 | } |
| 3021 | |
| 3022 | /* check for valid device id */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3023 | if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3024 | pr_err("tspp: Invalid device ID %d", pdev->id); |
| 3025 | rc = -EINVAL; |
| 3026 | goto out; |
| 3027 | } |
| 3028 | |
| 3029 | /* OK, we will use this device */ |
| 3030 | device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL); |
| 3031 | if (!device) { |
| 3032 | pr_err("tspp: Failed to allocate memory for device"); |
| 3033 | rc = -ENOMEM; |
| 3034 | goto out; |
| 3035 | } |
| 3036 | |
| 3037 | /* set up references */ |
| 3038 | device->pdev = pdev; |
| 3039 | platform_set_drvdata(pdev, device); |
| 3040 | |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 3041 | /* register bus client */ |
| 3042 | if (tspp_bus_pdata) { |
| 3043 | device->tsif_bus_client = |
| 3044 | msm_bus_scale_register_client(tspp_bus_pdata); |
| 3045 | if (!device->tsif_bus_client) |
| 3046 | pr_err("tspp: Unable to register bus client\n"); |
| 3047 | } else { |
| 3048 | device->tsif_bus_client = 0; |
| 3049 | } |
| 3050 | |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 3051 | /* map regulators */ |
| 3052 | if (data->tsif_vreg_present) { |
| 3053 | device->tsif_vreg = devm_regulator_get(&pdev->dev, "vdd_cx"); |
| 3054 | if (IS_ERR(device->tsif_vreg)) { |
| 3055 | rc = PTR_ERR(device->tsif_vreg); |
| 3056 | device->tsif_vreg = NULL; |
| 3057 | goto err_regultaor; |
| 3058 | } |
| 3059 | |
| 3060 | /* Set an initial voltage and enable the regulator */ |
| 3061 | rc = regulator_set_voltage(device->tsif_vreg, |
Hamad Kadmany | fde2938 | 2013-06-26 08:03:51 +0300 | [diff] [blame] | 3062 | RPM_REGULATOR_CORNER_NONE, |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 3063 | RPM_REGULATOR_CORNER_SUPER_TURBO); |
| 3064 | if (rc) { |
| 3065 | dev_err(&pdev->dev, "Unable to set CX voltage.\n"); |
| 3066 | goto err_regultaor; |
| 3067 | } |
| 3068 | |
| 3069 | rc = regulator_enable(device->tsif_vreg); |
| 3070 | if (rc) { |
| 3071 | dev_err(&pdev->dev, "Unable to enable CX regulator.\n"); |
| 3072 | goto err_regultaor; |
| 3073 | } |
| 3074 | } |
| 3075 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3076 | /* map clocks */ |
| 3077 | if (data->tsif_pclk) { |
Joel Nider | b9662ca | 2012-06-10 14:21:11 +0300 | [diff] [blame] | 3078 | device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3079 | if (IS_ERR(device->tsif_pclk)) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3080 | rc = PTR_ERR(device->tsif_pclk); |
| 3081 | device->tsif_pclk = NULL; |
| 3082 | goto err_pclock; |
| 3083 | } |
| 3084 | } |
| 3085 | if (data->tsif_ref_clk) { |
Joel Nider | b9662ca | 2012-06-10 14:21:11 +0300 | [diff] [blame] | 3086 | device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3087 | if (IS_ERR(device->tsif_ref_clk)) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3088 | rc = PTR_ERR(device->tsif_ref_clk); |
| 3089 | device->tsif_ref_clk = NULL; |
| 3090 | goto err_refclock; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | /* map I/O memory */ |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3095 | mem_tsif0 = platform_get_resource_byname(pdev, |
| 3096 | IORESOURCE_MEM, "MSM_TSIF0_PHYS"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3097 | if (!mem_tsif0) { |
| 3098 | pr_err("tspp: Missing tsif0 MEM resource"); |
| 3099 | rc = -ENXIO; |
| 3100 | goto err_res_tsif0; |
| 3101 | } |
| 3102 | device->tsif[0].base = ioremap(mem_tsif0->start, |
| 3103 | resource_size(mem_tsif0)); |
| 3104 | if (!device->tsif[0].base) { |
| 3105 | pr_err("tspp: ioremap failed"); |
| 3106 | goto err_map_tsif0; |
| 3107 | } |
| 3108 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3109 | mem_tsif1 = platform_get_resource_byname(pdev, |
| 3110 | IORESOURCE_MEM, "MSM_TSIF1_PHYS"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3111 | if (!mem_tsif1) { |
| 3112 | dev_err(&pdev->dev, "Missing tsif1 MEM resource"); |
| 3113 | rc = -ENXIO; |
| 3114 | goto err_res_tsif1; |
| 3115 | } |
| 3116 | device->tsif[1].base = ioremap(mem_tsif1->start, |
| 3117 | resource_size(mem_tsif1)); |
| 3118 | if (!device->tsif[1].base) { |
| 3119 | dev_err(&pdev->dev, "ioremap failed"); |
| 3120 | goto err_map_tsif1; |
| 3121 | } |
| 3122 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3123 | mem_tspp = platform_get_resource_byname(pdev, |
| 3124 | IORESOURCE_MEM, "MSM_TSPP_PHYS"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3125 | if (!mem_tspp) { |
| 3126 | dev_err(&pdev->dev, "Missing MEM resource"); |
| 3127 | rc = -ENXIO; |
| 3128 | goto err_res_dev; |
| 3129 | } |
| 3130 | device->base = ioremap(mem_tspp->start, resource_size(mem_tspp)); |
| 3131 | if (!device->base) { |
| 3132 | dev_err(&pdev->dev, "ioremap failed"); |
| 3133 | goto err_map_dev; |
| 3134 | } |
| 3135 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3136 | mem_bam = platform_get_resource_byname(pdev, |
| 3137 | IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3138 | if (!mem_bam) { |
| 3139 | pr_err("tspp: Missing bam MEM resource"); |
| 3140 | rc = -ENXIO; |
| 3141 | goto err_res_bam; |
| 3142 | } |
| 3143 | memset(&device->bam_props, 0, sizeof(device->bam_props)); |
| 3144 | device->bam_props.phys_addr = mem_bam->start; |
| 3145 | device->bam_props.virt_addr = ioremap(mem_bam->start, |
| 3146 | resource_size(mem_bam)); |
| 3147 | if (!device->bam_props.virt_addr) { |
| 3148 | dev_err(&pdev->dev, "ioremap failed"); |
| 3149 | goto err_map_bam; |
| 3150 | } |
| 3151 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3152 | if (msm_tspp_map_irqs(pdev, device)) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3153 | goto err_irq; |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 3154 | device->req_irqs = false; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3155 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3156 | /* power management */ |
| 3157 | pm_runtime_set_active(&pdev->dev); |
| 3158 | pm_runtime_enable(&pdev->dev); |
| 3159 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3160 | tspp_debugfs_init(device, 0); |
| 3161 | |
| 3162 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 3163 | tsif_debugfs_init(&device->tsif[i], i); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3164 | |
| 3165 | wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND, |
| 3166 | dev_name(&pdev->dev)); |
| 3167 | |
| 3168 | /* set up pointers to ram-based 'registers' */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3169 | device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0; |
| 3170 | device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1; |
| 3171 | device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2; |
| 3172 | device->tspp_key_table = device->base + TSPP_DATA_KEY; |
| 3173 | device->tspp_global_performance = |
| 3174 | device->base + TSPP_GLOBAL_PERFORMANCE; |
| 3175 | device->tspp_pipe_context = |
| 3176 | device->base + TSPP_PIPE_CONTEXT; |
| 3177 | device->tspp_pipe_performance = |
| 3178 | device->base + TSPP_PIPE_PERFORMANCE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3179 | |
| 3180 | device->bam_props.summing_threshold = 0x10; |
| 3181 | device->bam_props.irq = device->bam_irq; |
| 3182 | device->bam_props.manage = SPS_BAM_MGR_LOCAL; |
| 3183 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3184 | if (tspp_clock_start(device) != 0) { |
| 3185 | dev_err(&pdev->dev, "Can't start clocks"); |
| 3186 | goto err_clock; |
| 3187 | } |
| 3188 | |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 3189 | device->bam_handle = SPS_DEV_HANDLE_INVALID; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3190 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3191 | spin_lock_init(&device->spinlock); |
| 3192 | tasklet_init(&device->tlet, tspp_sps_complete_tlet, |
| 3193 | (unsigned long)device); |
| 3194 | |
| 3195 | /* initialize everything to a known state */ |
| 3196 | tspp_global_reset(device); |
| 3197 | |
| 3198 | version = readl_relaxed(device->base + TSPP_VERSION); |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3199 | /* |
| 3200 | * TSPP version can be bits [7:0] or alternatively, |
| 3201 | * TSPP major version is bits [31:28]. |
| 3202 | */ |
| 3203 | if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1)) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3204 | pr_warn("tspp: unrecognized hw version=%i", version); |
| 3205 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3206 | /* initialize the channels */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3207 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3208 | if (tspp_channel_init(&(device->channels[i]), device) != 0) { |
| 3209 | pr_err("tspp_channel_init failed"); |
| 3210 | goto err_channel; |
| 3211 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3212 | } |
| 3213 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3214 | /* stop the clocks for power savings */ |
| 3215 | tspp_clock_stop(device); |
| 3216 | |
| 3217 | /* everything is ok, so add the device to the list */ |
| 3218 | list_add_tail(&(device->devlist), &tspp_devices); |
| 3219 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3220 | return 0; |
| 3221 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3222 | err_channel: |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3223 | /* un-initialize channels */ |
Liron Kuch | 72b7855 | 2012-10-30 17:47:50 +0200 | [diff] [blame] | 3224 | for (j = 0; j < i; j++) { |
| 3225 | channel = &(device->channels[i]); |
| 3226 | device_destroy(tspp_class, channel->cdev.dev); |
| 3227 | cdev_del(&channel->cdev); |
| 3228 | } |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3229 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3230 | sps_deregister_bam_device(device->bam_handle); |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3231 | err_clock: |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3232 | tspp_debugfs_exit(device); |
| 3233 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 3234 | tsif_debugfs_exit(&device->tsif[i]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3235 | err_irq: |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3236 | iounmap(device->bam_props.virt_addr); |
| 3237 | err_map_bam: |
| 3238 | err_res_bam: |
| 3239 | iounmap(device->base); |
| 3240 | err_map_dev: |
| 3241 | err_res_dev: |
| 3242 | iounmap(device->tsif[1].base); |
| 3243 | err_map_tsif1: |
| 3244 | err_res_tsif1: |
| 3245 | iounmap(device->tsif[0].base); |
| 3246 | err_map_tsif0: |
| 3247 | err_res_tsif0: |
| 3248 | if (device->tsif_ref_clk) |
| 3249 | clk_put(device->tsif_ref_clk); |
| 3250 | err_refclock: |
| 3251 | if (device->tsif_pclk) |
| 3252 | clk_put(device->tsif_pclk); |
| 3253 | err_pclock: |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 3254 | if (device->tsif_vreg) |
| 3255 | regulator_disable(device->tsif_vreg); |
| 3256 | err_regultaor: |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 3257 | if (device->tsif_bus_client) |
| 3258 | msm_bus_scale_unregister_client(device->tsif_bus_client); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3259 | kfree(device); |
| 3260 | |
| 3261 | out: |
| 3262 | return rc; |
| 3263 | } |
| 3264 | |
| 3265 | static int __devexit msm_tspp_remove(struct platform_device *pdev) |
| 3266 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3267 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3268 | u32 i; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3269 | |
| 3270 | struct tspp_device *device = platform_get_drvdata(pdev); |
| 3271 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3272 | /* free the buffers, and delete the channels */ |
| 3273 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) { |
| 3274 | channel = &device->channels[i]; |
| 3275 | tspp_close_channel(device->pdev->id, i); |
| 3276 | device_destroy(tspp_class, channel->cdev.dev); |
| 3277 | cdev_del(&channel->cdev); |
| 3278 | } |
| 3279 | |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 3280 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3281 | tsif_debugfs_exit(&device->tsif[i]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3282 | |
Hamad Kadmany | ef5ce99 | 2013-06-03 09:30:50 +0300 | [diff] [blame] | 3283 | if (device->tsif_bus_client) |
| 3284 | msm_bus_scale_unregister_client(device->tsif_bus_client); |
| 3285 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3286 | wake_lock_destroy(&device->wake_lock); |
Gilad Broner | f0f326c | 2014-06-23 17:56:17 +0300 | [diff] [blame] | 3287 | if (device->req_irqs) |
| 3288 | msm_tspp_free_irqs(device); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3289 | |
| 3290 | iounmap(device->bam_props.virt_addr); |
| 3291 | iounmap(device->base); |
| 3292 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 3293 | iounmap(device->tsif[i].base); |
| 3294 | |
| 3295 | if (device->tsif_ref_clk) |
| 3296 | clk_put(device->tsif_ref_clk); |
| 3297 | |
| 3298 | if (device->tsif_pclk) |
| 3299 | clk_put(device->tsif_pclk); |
| 3300 | |
Vikram Mulukutla | e9ca54b | 2013-04-19 11:08:42 -0700 | [diff] [blame] | 3301 | if (device->tsif_vreg) |
| 3302 | regulator_disable(device->tsif_vreg); |
| 3303 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3304 | pm_runtime_disable(&pdev->dev); |
Liron Kuch | 605cc12 | 2013-02-21 14:25:57 +0200 | [diff] [blame] | 3305 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3306 | kfree(device); |
| 3307 | |
| 3308 | return 0; |
| 3309 | } |
| 3310 | |
| 3311 | /*** power management ***/ |
| 3312 | |
| 3313 | static int tspp_runtime_suspend(struct device *dev) |
| 3314 | { |
| 3315 | dev_dbg(dev, "pm_runtime: suspending..."); |
| 3316 | return 0; |
| 3317 | } |
| 3318 | |
| 3319 | static int tspp_runtime_resume(struct device *dev) |
| 3320 | { |
| 3321 | dev_dbg(dev, "pm_runtime: resuming..."); |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
| 3325 | static const struct dev_pm_ops tspp_dev_pm_ops = { |
| 3326 | .runtime_suspend = tspp_runtime_suspend, |
| 3327 | .runtime_resume = tspp_runtime_resume, |
| 3328 | }; |
| 3329 | |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3330 | static struct of_device_id msm_match_table[] = { |
| 3331 | {.compatible = "qcom,msm_tspp"}, |
| 3332 | {} |
| 3333 | }; |
| 3334 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3335 | static struct platform_driver msm_tspp_driver = { |
| 3336 | .probe = msm_tspp_probe, |
| 3337 | .remove = __exit_p(msm_tspp_remove), |
| 3338 | .driver = { |
| 3339 | .name = "msm_tspp", |
| 3340 | .pm = &tspp_dev_pm_ops, |
Liron Kuch | 5933992 | 2013-01-01 18:29:47 +0200 | [diff] [blame] | 3341 | .of_match_table = msm_match_table, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3342 | }, |
| 3343 | }; |
| 3344 | |
| 3345 | |
| 3346 | static int __init mod_init(void) |
| 3347 | { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3348 | int rc; |
| 3349 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3350 | /* make the char devs (channels) */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3351 | rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp"); |
| 3352 | if (rc) { |
| 3353 | pr_err("tspp: alloc_chrdev_region failed: %d", rc); |
| 3354 | goto err_devrgn; |
| 3355 | } |
| 3356 | |
| 3357 | tspp_class = class_create(THIS_MODULE, "tspp"); |
| 3358 | if (IS_ERR(tspp_class)) { |
| 3359 | rc = PTR_ERR(tspp_class); |
| 3360 | pr_err("tspp: Error creating class: %d", rc); |
| 3361 | goto err_class; |
| 3362 | } |
| 3363 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3364 | /* register the driver, and check hardware */ |
| 3365 | rc = platform_driver_register(&msm_tspp_driver); |
| 3366 | if (rc) { |
| 3367 | pr_err("tspp: platform_driver_register failed: %d", rc); |
| 3368 | goto err_register; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | return 0; |
| 3372 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3373 | err_register: |
| 3374 | class_destroy(tspp_class); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3375 | err_class: |
| 3376 | unregister_chrdev_region(0, TSPP_NUM_CHANNELS); |
| 3377 | err_devrgn: |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3378 | return rc; |
| 3379 | } |
| 3380 | |
| 3381 | static void __exit mod_exit(void) |
| 3382 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3383 | /* delete low level driver */ |
| 3384 | platform_driver_unregister(&msm_tspp_driver); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3385 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3386 | /* delete upper layer interface */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3387 | class_destroy(tspp_class); |
| 3388 | unregister_chrdev_region(0, TSPP_NUM_CHANNELS); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | module_init(mod_init); |
| 3392 | module_exit(mod_exit); |
| 3393 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 3394 | MODULE_DESCRIPTION("TSPP platform device and char dev"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 3395 | MODULE_LICENSE("GPL v2"); |