Joel Nider | b9662ca | 2012-06-10 14:21:11 +0300 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. 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 */ |
| 28 | #include <linux/delay.h> /* msleep */ |
| 29 | #include <linux/platform_device.h> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 30 | #include <linux/clk.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 31 | #include <linux/poll.h> /* poll() file op */ |
| 32 | #include <linux/wait.h> /* wait() macros, sleeping */ |
| 33 | #include <linux/tspp.h> /* tspp functions */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 34 | #include <linux/bitops.h> /* BIT() macro */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 35 | #include <mach/sps.h> /* BAM stuff */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 36 | #include <mach/gpio.h> |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 37 | #include <linux/wakelock.h> /* Locking functions */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 38 | #include <mach/dma.h> |
| 39 | #include <mach/msm_tspp.h> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 40 | #include <linux/debugfs.h> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * General defines |
| 44 | */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 45 | #define TSPP_TSIF_INSTANCES 2 |
| 46 | #define TSPP_FILTER_TABLES 3 |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 47 | #define TSPP_MAX_DEVICES 1 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 48 | #define TSPP_NUM_CHANNELS 16 |
| 49 | #define TSPP_NUM_PRIORITIES 16 |
| 50 | #define TSPP_NUM_KEYS 8 |
| 51 | #define INVALID_CHANNEL 0xFFFFFFFF |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 52 | #define TSPP_SPS_DESCRIPTOR_COUNT 128 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 53 | #define TSPP_PACKET_LENGTH 188 |
| 54 | #define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 55 | #define TSPP_MAX_BUFFER_SIZE (32 * 1024) |
| 56 | #define TSPP_NUM_BUFFERS 64 |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 57 | #define TSPP_TSIF_DEFAULT_TIME_LIMIT 60 |
| 58 | #define SPS_DESCRIPTOR_SIZE 8 |
| 59 | #define MIN_ACCEPTABLE_BUFFER_COUNT 2 |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 60 | #define TSPP_DEBUG(msg...) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * TSIF register offsets |
| 64 | */ |
| 65 | #define TSIF_STS_CTL_OFF (0x0) |
| 66 | #define TSIF_TIME_LIMIT_OFF (0x4) |
| 67 | #define TSIF_CLK_REF_OFF (0x8) |
| 68 | #define TSIF_LPBK_FLAGS_OFF (0xc) |
| 69 | #define TSIF_LPBK_DATA_OFF (0x10) |
| 70 | #define TSIF_TEST_CTL_OFF (0x14) |
| 71 | #define TSIF_TEST_MODE_OFF (0x18) |
| 72 | #define TSIF_TEST_RESET_OFF (0x1c) |
| 73 | #define TSIF_TEST_EXPORT_OFF (0x20) |
| 74 | #define TSIF_TEST_CURRENT_OFF (0x24) |
| 75 | |
| 76 | #define TSIF_DATA_PORT_OFF (0x100) |
| 77 | |
| 78 | /* bits for TSIF_STS_CTL register */ |
| 79 | #define TSIF_STS_CTL_EN_IRQ BIT(28) |
| 80 | #define TSIF_STS_CTL_PACK_AVAIL BIT(27) |
| 81 | #define TSIF_STS_CTL_1ST_PACKET BIT(26) |
| 82 | #define TSIF_STS_CTL_OVERFLOW BIT(25) |
| 83 | #define TSIF_STS_CTL_LOST_SYNC BIT(24) |
| 84 | #define TSIF_STS_CTL_TIMEOUT BIT(23) |
| 85 | #define TSIF_STS_CTL_INV_SYNC BIT(21) |
| 86 | #define TSIF_STS_CTL_INV_NULL BIT(20) |
| 87 | #define TSIF_STS_CTL_INV_ERROR BIT(19) |
| 88 | #define TSIF_STS_CTL_INV_ENABLE BIT(18) |
| 89 | #define TSIF_STS_CTL_INV_DATA BIT(17) |
| 90 | #define TSIF_STS_CTL_INV_CLOCK BIT(16) |
| 91 | #define TSIF_STS_CTL_SPARE BIT(15) |
| 92 | #define TSIF_STS_CTL_EN_NULL BIT(11) |
| 93 | #define TSIF_STS_CTL_EN_ERROR BIT(10) |
| 94 | #define TSIF_STS_CTL_LAST_BIT BIT(9) |
| 95 | #define TSIF_STS_CTL_EN_TIME_LIM BIT(8) |
| 96 | #define TSIF_STS_CTL_EN_TCR BIT(7) |
| 97 | #define TSIF_STS_CTL_TEST_MODE BIT(6) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 98 | #define TSIF_STS_CTL_MODE_2 BIT(5) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 99 | #define TSIF_STS_CTL_EN_DM BIT(4) |
| 100 | #define TSIF_STS_CTL_STOP BIT(3) |
| 101 | #define TSIF_STS_CTL_START BIT(0) |
| 102 | |
| 103 | /* |
| 104 | * TSPP register offsets |
| 105 | */ |
| 106 | #define TSPP_RST 0x00 |
| 107 | #define TSPP_CLK_CONTROL 0x04 |
| 108 | #define TSPP_CONFIG 0x08 |
| 109 | #define TSPP_CONTROL 0x0C |
| 110 | #define TSPP_PS_DISABLE 0x10 |
| 111 | #define TSPP_MSG_IRQ_STATUS 0x14 |
| 112 | #define TSPP_MSG_IRQ_MASK 0x18 |
| 113 | #define TSPP_IRQ_STATUS 0x1C |
| 114 | #define TSPP_IRQ_MASK 0x20 |
| 115 | #define TSPP_IRQ_CLEAR 0x24 |
| 116 | #define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2)) |
| 117 | #define TSPP_STATUS 0x68 |
| 118 | #define TSPP_CURR_TSP_HEADER 0x6C |
| 119 | #define TSPP_CURR_PID_FILTER 0x70 |
| 120 | #define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2)) |
| 121 | #define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2)) |
| 122 | #define TSPP_DATA_KEY_RESET 0x9C |
| 123 | #define TSPP_KEY_VALID 0xA0 |
| 124 | #define TSPP_KEY_ERROR 0xA4 |
| 125 | #define TSPP_TEST_CTRL 0xA8 |
| 126 | #define TSPP_VERSION 0xAC |
| 127 | #define TSPP_GENERICS 0xB0 |
| 128 | #define TSPP_NOP 0xB4 |
| 129 | |
| 130 | /* |
| 131 | * Register bit definitions |
| 132 | */ |
| 133 | /* TSPP_RST */ |
| 134 | #define TSPP_RST_RESET BIT(0) |
| 135 | |
| 136 | /* TSPP_CLK_CONTROL */ |
| 137 | #define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9) |
| 138 | #define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8) |
| 139 | #define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7) |
| 140 | #define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6) |
| 141 | #define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5) |
| 142 | #define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4) |
| 143 | #define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3) |
| 144 | #define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2) |
| 145 | #define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1) |
| 146 | #define TSPP_CLK_CONTROL_SET_CLKON BIT(0) |
| 147 | |
| 148 | /* TSPP_CONFIG */ |
| 149 | #define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \ |
| 150 | ((_b & 0xF) << 8)) |
| 151 | #define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF) |
| 152 | #define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7) |
| 153 | #define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6) |
| 154 | #define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5) |
| 155 | #define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4) |
| 156 | #define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3) |
| 157 | #define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2) |
| 158 | #define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1) |
| 159 | #define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0) |
| 160 | |
| 161 | /* TSPP_CONTROL */ |
| 162 | #define TSPP_CONTROL_PID_FILTER_LOCK BIT(5) |
| 163 | #define TSPP_CONTROL_FORCE_KEY_CALC BIT(4) |
| 164 | #define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3) |
| 165 | #define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2) |
| 166 | #define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1) |
| 167 | #define TSPP_CONTROL_PERF_COUNT_INIT BIT(0) |
| 168 | |
| 169 | /* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */ |
| 170 | #define TSPP_MSG_TSPP_IRQ BIT(2) |
| 171 | #define TSPP_MSG_TSIF_1_IRQ BIT(1) |
| 172 | #define TSPP_MSG_TSIF_0_IRQ BIT(0) |
| 173 | |
| 174 | /* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */ |
| 175 | #define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19) |
| 176 | #define TSPP_IRQ_STATUS_KEY_ERROR BIT(18) |
| 177 | #define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17) |
| 178 | #define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16) |
| 179 | #define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n)) |
| 180 | |
| 181 | /* TSPP_PIPE_ERROR_STATUS */ |
| 182 | #define TSPP_PIPE_PES_SYNC_ERROR BIT(3) |
| 183 | #define TSPP_PIPE_PS_LENGTH_ERROR BIT(2) |
| 184 | #define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1) |
| 185 | #define TSPP_PIP_PS_LOST_START BIT(0) |
| 186 | |
| 187 | /* TSPP_STATUS */ |
| 188 | #define TSPP_STATUS_TSP_PKT_AVAIL BIT(10) |
| 189 | #define TSPP_STATUS_TSIF1_DM_REQ BIT(6) |
| 190 | #define TSPP_STATUS_TSIF0_DM_REQ BIT(2) |
| 191 | #define TSPP_CURR_FILTER_TABLE BIT(0) |
| 192 | |
| 193 | /* TSPP_GENERICS */ |
| 194 | #define TSPP_GENERICS_CRYPTO_GEN BIT(12) |
| 195 | #define TSPP_GENERICS_MAX_CONS_PIPES BIT(7) |
| 196 | #define TSPP_GENERICS_MAX_PIPES BIT(2) |
| 197 | #define TSPP_GENERICS_TSIF_1_GEN BIT(1) |
| 198 | #define TSPP_GENERICS_TSIF_0_GEN BIT(0) |
| 199 | |
| 200 | /* |
| 201 | * TSPP memory regions |
| 202 | */ |
| 203 | #define TSPP_PID_FILTER_TABLE0 0x800 |
| 204 | #define TSPP_PID_FILTER_TABLE1 0x880 |
| 205 | #define TSPP_PID_FILTER_TABLE2 0x900 |
| 206 | #define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */ |
| 207 | #define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */ |
| 208 | #define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */ |
| 209 | #define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2)) |
| 210 | #define TSPP_DATA_KEY 0xCD0 |
| 211 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 212 | struct debugfs_entry { |
| 213 | const char *name; |
| 214 | mode_t mode; |
| 215 | int offset; |
| 216 | }; |
| 217 | |
| 218 | static const struct debugfs_entry debugfs_tsif_regs[] = { |
| 219 | {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF}, |
| 220 | {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF}, |
| 221 | {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF}, |
| 222 | {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF}, |
| 223 | {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF}, |
| 224 | {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF}, |
| 225 | {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF}, |
| 226 | {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF}, |
| 227 | {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF}, |
| 228 | {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF}, |
| 229 | {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF}, |
| 230 | }; |
| 231 | |
| 232 | static const struct debugfs_entry debugfs_tspp_regs[] = { |
| 233 | {"rst", S_IRUGO | S_IWUSR, TSPP_RST}, |
| 234 | {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL}, |
| 235 | {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG}, |
| 236 | {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL}, |
| 237 | {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE}, |
| 238 | {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS}, |
| 239 | {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK}, |
| 240 | {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS}, |
| 241 | {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK}, |
| 242 | {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR}, |
| 243 | /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */ |
| 244 | {"status", S_IRUGO | S_IWUSR, TSPP_STATUS}, |
| 245 | {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER}, |
| 246 | {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER}, |
| 247 | /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */ |
| 248 | /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */ |
| 249 | {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET}, |
| 250 | {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID}, |
| 251 | {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR}, |
| 252 | {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL}, |
| 253 | {"version", S_IRUGO | S_IWUSR, TSPP_VERSION}, |
| 254 | {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS}, |
| 255 | {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0}, |
| 256 | {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1}, |
| 257 | {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2}, |
| 258 | {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE}, |
| 259 | {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT}, |
| 260 | {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE}, |
| 261 | {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY} |
| 262 | }; |
| 263 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 264 | struct tspp_pid_filter { |
| 265 | u32 filter; /* see FILTER_ macros */ |
| 266 | u32 config; /* see FILTER_ macros */ |
| 267 | }; |
| 268 | |
| 269 | /* tsp_info */ |
| 270 | #define FILTER_HEADER_ERROR_MASK BIT(7) |
| 271 | #define FILTER_TRANS_END_DISABLE BIT(6) |
| 272 | #define FILTER_DEC_ON_ERROR_EN BIT(5) |
| 273 | #define FILTER_DECRYPT BIT(4) |
| 274 | #define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT) |
| 275 | #define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF) |
| 276 | #define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \ |
| 277 | (_p->config & ~0xF) | (_b & 0xF)) |
| 278 | #define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3) |
| 279 | #define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \ |
| 280 | (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30)) |
| 281 | #define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF) |
| 282 | #define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \ |
| 283 | (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13)) |
| 284 | #define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF) |
| 285 | #define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \ |
| 286 | (_p->filter & ~0x1FFF) | (_b & 0x1FFF)) |
| 287 | #define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3) |
| 288 | #define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \ |
| 289 | (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30)) |
| 290 | #define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7) |
| 291 | #define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \ |
| 292 | (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8)) |
| 293 | |
| 294 | struct tspp_global_performance_regs { |
| 295 | u32 tsp_total; |
| 296 | u32 tsp_ignored; |
| 297 | u32 tsp_error; |
| 298 | u32 tsp_sync; |
| 299 | }; |
| 300 | |
| 301 | struct tspp_pipe_context_regs { |
| 302 | u16 pes_bytes_left; |
| 303 | u16 count; |
| 304 | u32 tsif_suffix; |
| 305 | } __packed; |
| 306 | #define CONTEXT_GET_STATE(_a) (_a & 0x3) |
| 307 | #define CONTEXT_UNSPEC_LENGTH BIT(11) |
| 308 | #define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF) |
| 309 | |
| 310 | struct tspp_pipe_performance_regs { |
| 311 | u32 tsp_total; |
| 312 | u32 ps_duplicate_tsp; |
| 313 | u32 tsp_no_payload; |
| 314 | u32 tsp_broken_ps; |
| 315 | u32 ps_total_num; |
| 316 | u32 ps_continuity_error; |
| 317 | u32 ps_length_error; |
| 318 | u32 pes_sync_error; |
| 319 | }; |
| 320 | |
| 321 | struct tspp_tsif_device { |
| 322 | void __iomem *base; |
| 323 | u32 time_limit; |
| 324 | u32 ref_count; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 325 | enum tspp_tsif_mode mode; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 326 | |
| 327 | /* debugfs */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 328 | struct dentry *dent_tsif; |
| 329 | struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | enum tspp_buf_state { |
| 333 | TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */ |
| 334 | TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 335 | TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */ |
| 336 | TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | struct tspp_mem_buffer { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 340 | struct tspp_mem_buffer *next; |
| 341 | struct sps_mem_buffer sps; |
| 342 | struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 343 | enum tspp_buf_state state; |
| 344 | size_t filled; /* how much data this buffer is holding */ |
| 345 | int read_index; /* where to start reading data from */ |
| 346 | }; |
| 347 | |
| 348 | /* this represents each char device 'channel' */ |
| 349 | struct tspp_channel { |
| 350 | struct cdev cdev; |
| 351 | struct device *dd; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 352 | struct tspp_device *pdev; /* can use container_of instead? */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 353 | struct sps_pipe *pipe; |
| 354 | struct sps_connect config; |
| 355 | struct sps_register_event event; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 356 | struct tspp_mem_buffer *data; /* list of buffers */ |
| 357 | struct tspp_mem_buffer *read; /* first buffer ready to be read */ |
| 358 | struct tspp_mem_buffer *waiting; /* first outstanding transfer */ |
| 359 | struct tspp_mem_buffer *locked; /* buffer currently being read */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 360 | wait_queue_head_t in_queue; /* set when data is received */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 361 | u32 id; /* channel id (0-15) */ |
| 362 | int used; /* is this channel in use? */ |
| 363 | int key; /* which encryption key index is used */ |
| 364 | u32 buffer_size; /* size of the sps transfer buffers */ |
| 365 | u32 max_buffers; /* how many buffers should be allocated */ |
| 366 | u32 buffer_count; /* how many buffers are actually allocated */ |
| 367 | u32 filter_count; /* how many filters have been added to this channel */ |
| 368 | u32 int_freq; /* generate interrupts every x descriptors */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 369 | enum tspp_source src; |
| 370 | enum tspp_mode mode; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 371 | tspp_notifier *notifier; /* used only with kernel api */ |
| 372 | void *notify_data; /* data to be passed with the notifier */ |
| 373 | u32 notify_timer; /* notification for partially filled buffers */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | struct tspp_pid_filter_table { |
| 377 | struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES]; |
| 378 | }; |
| 379 | |
| 380 | struct tspp_key_entry { |
| 381 | u32 even_lsb; |
| 382 | u32 even_msb; |
| 383 | u32 odd_lsb; |
| 384 | u32 odd_msb; |
| 385 | }; |
| 386 | |
| 387 | struct tspp_key_table { |
| 388 | struct tspp_key_entry entry[TSPP_NUM_KEYS]; |
| 389 | }; |
| 390 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 391 | /* this represents the actual hardware device */ |
| 392 | struct tspp_device { |
| 393 | struct list_head devlist; /* list of all devices */ |
| 394 | struct platform_device *pdev; |
| 395 | void __iomem *base; |
| 396 | unsigned int tspp_irq; |
| 397 | unsigned int bam_irq; |
| 398 | u32 bam_handle; |
| 399 | struct sps_bam_props bam_props; |
| 400 | struct wake_lock wake_lock; |
| 401 | spinlock_t spinlock; |
| 402 | struct tasklet_struct tlet; |
| 403 | struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES]; |
| 404 | /* clocks */ |
| 405 | struct clk *tsif_pclk; |
| 406 | struct clk *tsif_ref_clk; |
| 407 | /* data */ |
| 408 | struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES]; |
| 409 | struct tspp_channel channels[TSPP_NUM_CHANNELS]; |
| 410 | struct tspp_key_table *tspp_key_table; |
| 411 | struct tspp_global_performance_regs *tspp_global_performance; |
| 412 | struct tspp_pipe_context_regs *tspp_pipe_context; |
| 413 | struct tspp_pipe_performance_regs *tspp_pipe_performance; |
| 414 | |
| 415 | struct dentry *dent; |
| 416 | struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)]; |
| 417 | }; |
| 418 | |
| 419 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 420 | static struct class *tspp_class; |
| 421 | static int tspp_key_entry; |
| 422 | static dev_t tspp_minor; /* next minor number to assign */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 423 | |
| 424 | static LIST_HEAD(tspp_devices); |
| 425 | |
| 426 | /* forward declarations */ |
| 427 | static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *); |
| 428 | static ssize_t tspp_open(struct inode *inode, struct file *filp); |
| 429 | static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p); |
| 430 | static ssize_t tspp_release(struct inode *inode, struct file *filp); |
| 431 | static long tspp_ioctl(struct file *, unsigned int, unsigned long); |
| 432 | |
| 433 | /* file operations */ |
| 434 | static const struct file_operations tspp_fops = { |
| 435 | .owner = THIS_MODULE, |
| 436 | .read = tspp_read, |
| 437 | .open = tspp_open, |
| 438 | .poll = tspp_poll, |
| 439 | .release = tspp_release, |
| 440 | .unlocked_ioctl = tspp_ioctl, |
| 441 | }; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 442 | |
| 443 | /*** IRQ ***/ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 444 | static irqreturn_t tspp_isr(int irq, void *dev) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 445 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 446 | struct tspp_device *device = dev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 447 | u32 status, mask; |
| 448 | u32 data; |
| 449 | |
| 450 | status = readl_relaxed(device->base + TSPP_IRQ_STATUS); |
| 451 | mask = readl_relaxed(device->base + TSPP_IRQ_MASK); |
| 452 | status &= mask; |
| 453 | |
| 454 | if (!status) { |
| 455 | dev_warn(&device->pdev->dev, "Spurious interrupt"); |
| 456 | return IRQ_NONE; |
| 457 | } |
| 458 | |
| 459 | /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */ |
| 460 | |
| 461 | if (status & TSPP_IRQ_STATUS_KEY_ERROR) { |
| 462 | /* read the key error info */ |
| 463 | data = readl_relaxed(device->base + TSPP_KEY_ERROR); |
| 464 | dev_info(&device->pdev->dev, "key error 0x%x", data); |
| 465 | } |
| 466 | if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) { |
| 467 | data = readl_relaxed(device->base + TSPP_KEY_VALID); |
| 468 | dev_info(&device->pdev->dev, "key invalidated: 0x%x", data); |
| 469 | } |
| 470 | if (status & TSPP_IRQ_STATUS_KEY_SWITCHED) |
| 471 | dev_info(&device->pdev->dev, "key switched"); |
| 472 | |
| 473 | if (status & 0xffff) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 474 | dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 475 | |
| 476 | writel_relaxed(status, device->base + TSPP_IRQ_CLEAR); |
| 477 | wmb(); |
| 478 | return IRQ_HANDLED; |
| 479 | } |
| 480 | |
| 481 | /*** callbacks ***/ |
| 482 | static void tspp_sps_complete_cb(struct sps_event_notify *notify) |
| 483 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 484 | struct tspp_device *pdev = notify->user; |
| 485 | tasklet_schedule(&pdev->tlet); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /*** tasklet ***/ |
| 489 | static void tspp_sps_complete_tlet(unsigned long data) |
| 490 | { |
| 491 | int i; |
| 492 | int complete; |
| 493 | unsigned long flags; |
| 494 | struct sps_iovec iovec; |
| 495 | struct tspp_channel *channel; |
| 496 | struct tspp_device *device = (struct tspp_device *)data; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 497 | spin_lock_irqsave(&device->spinlock, flags); |
| 498 | |
| 499 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) { |
| 500 | complete = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 501 | channel = &device->channels[i]; |
| 502 | if (!channel->used || !channel->waiting) |
| 503 | continue; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 504 | |
| 505 | /* get completions */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 506 | while (channel->waiting->state == TSPP_BUF_STATE_WAITING) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 507 | if (sps_get_iovec(channel->pipe, &iovec) != 0) { |
| 508 | pr_err("tspp: Error in iovec on channel %i", |
| 509 | channel->id); |
| 510 | break; |
| 511 | } |
| 512 | if (iovec.size == 0) |
| 513 | break; |
| 514 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 515 | if (iovec.addr != channel->waiting->sps.phys_base) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 516 | pr_err("tspp: buffer mismatch 0x%08x", |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 517 | channel->waiting->sps.phys_base); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 518 | |
| 519 | complete = 1; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 520 | channel->waiting->state = TSPP_BUF_STATE_DATA; |
| 521 | channel->waiting->filled = iovec.size; |
| 522 | channel->waiting->read_index = 0; |
| 523 | |
| 524 | /* update the pointers */ |
| 525 | channel->waiting = channel->waiting->next; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 528 | /* wake any waiting processes */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 529 | if (complete) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 530 | wake_up_interruptible(&channel->in_queue); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 531 | |
| 532 | /* call notifiers */ |
| 533 | if (channel->notifier) |
| 534 | channel->notifier(channel->id, |
| 535 | channel->notify_data); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | spin_unlock_irqrestore(&device->spinlock, flags); |
| 540 | } |
| 541 | |
| 542 | /*** GPIO functions ***/ |
| 543 | static void tspp_gpios_free(const struct msm_gpio *table, int size) |
| 544 | { |
| 545 | int i; |
| 546 | const struct msm_gpio *g; |
| 547 | for (i = size-1; i >= 0; i--) { |
| 548 | g = table + i; |
| 549 | gpio_free(GPIO_PIN(g->gpio_cfg)); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static int tspp_gpios_request(const struct msm_gpio *table, int size) |
| 554 | { |
| 555 | int rc; |
| 556 | int i; |
| 557 | const struct msm_gpio *g; |
| 558 | for (i = 0; i < size; i++) { |
| 559 | g = table + i; |
| 560 | rc = gpio_request(GPIO_PIN(g->gpio_cfg), g->label); |
| 561 | if (rc) { |
| 562 | pr_err("tspp: gpio_request(%d) <%s> failed: %d\n", |
| 563 | GPIO_PIN(g->gpio_cfg), g->label ?: "?", rc); |
| 564 | goto err; |
| 565 | } |
| 566 | } |
| 567 | return 0; |
| 568 | err: |
| 569 | tspp_gpios_free(table, i); |
| 570 | return rc; |
| 571 | } |
| 572 | |
| 573 | static int tspp_gpios_disable(const struct msm_gpio *table, int size) |
| 574 | { |
| 575 | int rc = 0; |
| 576 | int i; |
| 577 | const struct msm_gpio *g; |
| 578 | for (i = size-1; i >= 0; i--) { |
| 579 | int tmp; |
| 580 | g = table + i; |
| 581 | tmp = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_DISABLE); |
| 582 | if (tmp) { |
| 583 | pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE)" |
| 584 | " <%s> failed: %d\n", |
| 585 | g->gpio_cfg, g->label ?: "?", rc); |
| 586 | pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n", |
| 587 | GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg), |
| 588 | GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg), |
| 589 | GPIO_DRVSTR(g->gpio_cfg)); |
| 590 | if (!rc) |
| 591 | rc = tmp; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return rc; |
| 596 | } |
| 597 | |
| 598 | static int tspp_gpios_enable(const struct msm_gpio *table, int size) |
| 599 | { |
| 600 | int rc; |
| 601 | int i; |
| 602 | const struct msm_gpio *g; |
| 603 | for (i = 0; i < size; i++) { |
| 604 | g = table + i; |
| 605 | rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE); |
| 606 | if (rc) { |
| 607 | pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE)" |
| 608 | " <%s> failed: %d\n", |
| 609 | g->gpio_cfg, g->label ?: "?", rc); |
| 610 | pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n", |
| 611 | GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg), |
| 612 | GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg), |
| 613 | GPIO_DRVSTR(g->gpio_cfg)); |
| 614 | goto err; |
| 615 | } |
| 616 | } |
| 617 | return 0; |
| 618 | err: |
| 619 | tspp_gpios_disable(table, i); |
| 620 | return rc; |
| 621 | } |
| 622 | |
| 623 | static int tspp_gpios_request_enable(const struct msm_gpio *table, int size) |
| 624 | { |
| 625 | int rc = tspp_gpios_request(table, size); |
| 626 | if (rc) |
| 627 | return rc; |
| 628 | rc = tspp_gpios_enable(table, size); |
| 629 | if (rc) |
| 630 | tspp_gpios_free(table, size); |
| 631 | return rc; |
| 632 | } |
| 633 | |
| 634 | static void tspp_gpios_disable_free(const struct msm_gpio *table, int size) |
| 635 | { |
| 636 | tspp_gpios_disable(table, size); |
| 637 | tspp_gpios_free(table, size); |
| 638 | } |
| 639 | |
| 640 | static int tspp_start_gpios(struct tspp_device *device) |
| 641 | { |
| 642 | struct msm_tspp_platform_data *pdata = |
| 643 | device->pdev->dev.platform_data; |
| 644 | return tspp_gpios_request_enable(pdata->gpios, pdata->num_gpios); |
| 645 | } |
| 646 | |
| 647 | static void tspp_stop_gpios(struct tspp_device *device) |
| 648 | { |
| 649 | struct msm_tspp_platform_data *pdata = |
| 650 | device->pdev->dev.platform_data; |
| 651 | tspp_gpios_disable_free(pdata->gpios, pdata->num_gpios); |
| 652 | } |
| 653 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 654 | /*** Clock functions ***/ |
| 655 | static int tspp_clock_start(struct tspp_device *device) |
| 656 | { |
| 657 | if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) { |
| 658 | pr_err("tspp: Can't start pclk"); |
| 659 | return -EBUSY; |
| 660 | } |
| 661 | |
| 662 | if (device->tsif_ref_clk && |
| 663 | clk_prepare_enable(device->tsif_ref_clk) != 0) { |
| 664 | pr_err("tspp: Can't start ref clk"); |
| 665 | clk_disable_unprepare(device->tsif_pclk); |
| 666 | return -EBUSY; |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static void tspp_clock_stop(struct tspp_device *device) |
| 673 | { |
| 674 | if (device->tsif_pclk) |
| 675 | clk_disable(device->tsif_pclk); |
| 676 | |
| 677 | if (device->tsif_ref_clk) |
| 678 | clk_disable(device->tsif_ref_clk); |
| 679 | } |
| 680 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 681 | /*** TSIF functions ***/ |
| 682 | static int tspp_start_tsif(struct tspp_tsif_device *tsif_device) |
| 683 | { |
| 684 | int start_hardware = 0; |
| 685 | u32 ctl; |
| 686 | |
| 687 | if (tsif_device->ref_count == 0) { |
| 688 | start_hardware = 1; |
| 689 | } else if (tsif_device->ref_count > 0) { |
| 690 | ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF); |
| 691 | if ((ctl & TSIF_STS_CTL_START) != 1) { |
| 692 | /* this hardware should already be running */ |
| 693 | pr_warn("tspp: tsif hw not started but ref count > 0"); |
| 694 | start_hardware = 1; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (start_hardware) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 699 | ctl = TSIF_STS_CTL_EN_IRQ | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 700 | TSIF_STS_CTL_EN_DM; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 701 | switch (tsif_device->mode) { |
| 702 | case TSPP_TSIF_MODE_LOOPBACK: |
| 703 | ctl |= TSIF_STS_CTL_EN_NULL | |
| 704 | TSIF_STS_CTL_EN_ERROR | |
| 705 | TSIF_STS_CTL_TEST_MODE; |
| 706 | break; |
| 707 | case TSPP_TSIF_MODE_1: |
| 708 | ctl |= TSIF_STS_CTL_EN_TIME_LIM | |
| 709 | TSIF_STS_CTL_EN_TCR; |
| 710 | break; |
| 711 | case TSPP_TSIF_MODE_2: |
| 712 | ctl |= TSIF_STS_CTL_EN_TIME_LIM | |
| 713 | TSIF_STS_CTL_EN_TCR | |
| 714 | TSIF_STS_CTL_MODE_2; |
| 715 | break; |
| 716 | default: |
| 717 | pr_warn("tspp: unknown tsif mode 0x%x", |
| 718 | tsif_device->mode); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 719 | } |
| 720 | writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF); |
| 721 | writel_relaxed(tsif_device->time_limit, |
| 722 | tsif_device->base + TSIF_TIME_LIMIT_OFF); |
| 723 | wmb(); |
| 724 | writel_relaxed(ctl | TSIF_STS_CTL_START, |
| 725 | tsif_device->base + TSIF_STS_CTL_OFF); |
| 726 | wmb(); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 729 | ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 730 | tsif_device->ref_count++; |
| 731 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 732 | return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device) |
| 736 | { |
| 737 | if (tsif_device->ref_count == 0) |
| 738 | return; |
| 739 | |
| 740 | tsif_device->ref_count--; |
| 741 | |
| 742 | if (tsif_device->ref_count == 0) { |
| 743 | writel_relaxed(TSIF_STS_CTL_STOP, |
| 744 | tsif_device->base + TSIF_STS_CTL_OFF); |
| 745 | wmb(); |
| 746 | } |
| 747 | } |
| 748 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 749 | /*** local TSPP functions ***/ |
| 750 | static int tspp_channels_in_use(struct tspp_device *pdev) |
| 751 | { |
| 752 | int i; |
| 753 | int count = 0; |
| 754 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) |
| 755 | count += (pdev->channels[i].used ? 1 : 0); |
| 756 | |
| 757 | return count; |
| 758 | } |
| 759 | |
| 760 | static struct tspp_device *tspp_find_by_id(int id) |
| 761 | { |
| 762 | struct tspp_device *dev; |
| 763 | list_for_each_entry(dev, &tspp_devices, devlist) { |
| 764 | if (dev->pdev->id == id) |
| 765 | return dev; |
| 766 | } |
| 767 | return NULL; |
| 768 | } |
| 769 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 770 | static int tspp_get_key_entry(void) |
| 771 | { |
| 772 | int i; |
| 773 | for (i = 0; i < TSPP_NUM_KEYS; i++) { |
| 774 | if (!(tspp_key_entry & (1 << i))) { |
| 775 | tspp_key_entry |= (1 << i); |
| 776 | return i; |
| 777 | } |
| 778 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 779 | return 1 < TSPP_NUM_KEYS; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static void tspp_free_key_entry(int entry) |
| 783 | { |
| 784 | if (entry > TSPP_NUM_KEYS) { |
| 785 | pr_err("tspp_free_key_entry: index out of bounds"); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | tspp_key_entry &= ~(1 << entry); |
| 790 | } |
| 791 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 792 | static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc, |
| 793 | u32 size, tspp_allocator *alloc, void *user) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 794 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 795 | if (size < TSPP_MIN_BUFFER_SIZE || |
| 796 | size > TSPP_MAX_BUFFER_SIZE) { |
| 797 | pr_err("tspp: bad buffer size %i", size); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 798 | return -ENOMEM; |
| 799 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 800 | |
| 801 | if (alloc) { |
| 802 | TSPP_DEBUG("tspp using alloc function"); |
| 803 | desc->virt_base = alloc(channel_id, size, |
| 804 | &desc->phys_base, user); |
| 805 | } else { |
| 806 | desc->virt_base = dma_alloc_coherent(NULL, size, |
| 807 | &desc->phys_base, GFP_KERNEL); |
| 808 | if (desc->virt_base == 0) { |
| 809 | pr_err("tspp dma alloc coherent failed %i", size); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 810 | return -ENOMEM; |
| 811 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | desc->size = size; |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static int tspp_queue_buffer(struct tspp_channel *channel, |
| 819 | struct tspp_mem_buffer *buffer) |
| 820 | { |
| 821 | int rc; |
| 822 | u32 flags = 0; |
| 823 | |
| 824 | /* make sure the interrupt frequency is valid */ |
| 825 | if (channel->int_freq < 1) |
| 826 | channel->int_freq = 1; |
| 827 | |
| 828 | /* generate interrupt according to requested frequency */ |
| 829 | if (buffer->desc.id % channel->int_freq == channel->int_freq-1) |
| 830 | flags = SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOB; |
| 831 | |
| 832 | /* start the transfer */ |
| 833 | rc = sps_transfer_one(channel->pipe, |
| 834 | buffer->sps.phys_base, |
| 835 | buffer->sps.size, |
| 836 | channel->pdev, |
| 837 | flags); |
| 838 | if (rc < 0) |
| 839 | return rc; |
| 840 | |
| 841 | buffer->state = TSPP_BUF_STATE_WAITING; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int tspp_global_reset(struct tspp_device *pdev) |
| 847 | { |
| 848 | u32 i, val; |
| 849 | |
| 850 | /* stop all TSIFs */ |
| 851 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) { |
| 852 | pdev->tsif[i].ref_count = 1; /* allows stopping hw */ |
| 853 | tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */ |
| 854 | pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT; |
| 855 | } |
| 856 | writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST); |
| 857 | wmb(); |
| 858 | |
| 859 | /* BAM */ |
| 860 | if (sps_device_reset(pdev->bam_handle) != 0) { |
| 861 | pr_err("tspp: error resetting bam"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 862 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* TSPP tables */ |
| 866 | for (i = 0; i < TSPP_FILTER_TABLES; i++) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 867 | memset(pdev->filters[i], |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 868 | 0, sizeof(struct tspp_pid_filter_table)); |
| 869 | |
| 870 | /* disable all filters */ |
| 871 | val = (2 << TSPP_NUM_CHANNELS) - 1; |
| 872 | writel_relaxed(val, pdev->base + TSPP_PS_DISABLE); |
| 873 | |
| 874 | /* TSPP registers */ |
| 875 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 876 | writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT, |
| 877 | pdev->base + TSPP_CONTROL); |
| 878 | wmb(); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 879 | memset(pdev->tspp_global_performance, 0, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 880 | sizeof(struct tspp_global_performance_regs)); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 881 | memset(pdev->tspp_pipe_context, 0, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 882 | sizeof(struct tspp_pipe_context_regs)); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 883 | memset(pdev->tspp_pipe_performance, 0, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 884 | sizeof(struct tspp_pipe_performance_regs)); |
| 885 | wmb(); |
| 886 | writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT, |
| 887 | pdev->base + TSPP_CONTROL); |
| 888 | wmb(); |
| 889 | |
| 890 | val = readl_relaxed(pdev->base + TSPP_CONFIG); |
| 891 | val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK | |
| 892 | TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK | |
| 893 | TSPP_CONFIG_PS_CONT_ERR_MASK); |
| 894 | TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH); |
| 895 | writel_relaxed(val, pdev->base + TSPP_CONFIG); |
| 896 | writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_MASK); |
| 897 | writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR); |
| 898 | writel_relaxed(0, pdev->base + TSPP_RST); |
| 899 | wmb(); |
| 900 | |
| 901 | tspp_key_entry = 0; |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 906 | static int tspp_select_source(u32 dev, u32 channel_id, |
| 907 | struct tspp_select_source *src) |
| 908 | { |
| 909 | /* make sure the requested src id is in bounds */ |
| 910 | if (src->source > TSPP_SOURCE_MEM) { |
| 911 | pr_err("tspp source out of bounds"); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | |
| 915 | /* open the stream */ |
| 916 | tspp_open_stream(dev, channel_id, src->source, src->mode); |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv) |
| 922 | { |
| 923 | struct tspp_device *pdev = channel->pdev; |
| 924 | |
| 925 | writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0)); |
| 926 | writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1)); |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static int tspp_set_system_keys(struct tspp_channel *channel, |
| 931 | struct tspp_system_keys *keys) |
| 932 | { |
| 933 | int i; |
| 934 | struct tspp_device *pdev = channel->pdev; |
| 935 | |
| 936 | for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++) |
| 937 | writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i)); |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | static int tspp_channel_init(struct tspp_channel *channel, |
| 943 | struct tspp_device *pdev) |
| 944 | { |
| 945 | channel->cdev.owner = THIS_MODULE; |
| 946 | cdev_init(&channel->cdev, &tspp_fops); |
| 947 | channel->pdev = pdev; |
| 948 | channel->data = NULL; |
| 949 | channel->read = NULL; |
| 950 | channel->waiting = NULL; |
| 951 | channel->locked = NULL; |
| 952 | channel->id = MINOR(tspp_minor); |
| 953 | channel->used = 0; |
| 954 | channel->buffer_size = TSPP_MIN_BUFFER_SIZE; |
| 955 | channel->max_buffers = TSPP_NUM_BUFFERS; |
| 956 | channel->buffer_count = 0; |
| 957 | channel->filter_count = 0; |
| 958 | channel->int_freq = 1; |
| 959 | channel->notifier = NULL; |
| 960 | channel->notify_data = NULL; |
| 961 | channel->notify_timer = 0; |
| 962 | init_waitqueue_head(&channel->in_queue); |
| 963 | |
| 964 | if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) { |
| 965 | pr_err("tspp: cdev_add failed"); |
| 966 | return -EBUSY; |
| 967 | } |
| 968 | |
| 969 | channel->dd = device_create(tspp_class, NULL, channel->cdev.dev, |
| 970 | channel, "tspp%02d", channel->id); |
| 971 | if (IS_ERR(channel->dd)) { |
| 972 | pr_err("tspp: device_create failed: %i", |
| 973 | (int)PTR_ERR(channel->dd)); |
| 974 | cdev_del(&channel->cdev); |
| 975 | return -EBUSY; |
| 976 | } |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | static int tspp_set_buffer_size(struct tspp_channel *channel, |
| 982 | struct tspp_buffer *buf) |
| 983 | { |
| 984 | if (buf->size < TSPP_MIN_BUFFER_SIZE) |
| 985 | channel->buffer_size = TSPP_MIN_BUFFER_SIZE; |
| 986 | else if (buf->size > TSPP_MAX_BUFFER_SIZE) |
| 987 | channel->buffer_size = TSPP_MAX_BUFFER_SIZE; |
| 988 | else |
| 989 | channel->buffer_size = buf->size; |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static void tspp_set_tsif_mode(struct tspp_channel *channel, |
| 995 | enum tspp_tsif_mode mode) |
| 996 | { |
| 997 | int index; |
| 998 | |
| 999 | switch (channel->src) { |
| 1000 | case TSPP_SOURCE_TSIF0: |
| 1001 | index = 0; |
| 1002 | break; |
| 1003 | case TSPP_SOURCE_TSIF1: |
| 1004 | index = 1; |
| 1005 | break; |
| 1006 | default: |
| 1007 | pr_warn("tspp: can't set mode for non-tsif source %d", |
| 1008 | channel->src); |
| 1009 | return; |
| 1010 | } |
| 1011 | channel->pdev->tsif[index].mode = mode; |
| 1012 | } |
| 1013 | |
| 1014 | /*** TSPP API functions ***/ |
| 1015 | int tspp_open_stream(u32 dev, u32 channel_id, enum tspp_source src, enum tspp_tsif_mode mode) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1016 | { |
| 1017 | u32 val; |
| 1018 | struct tspp_device *pdev; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1019 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1020 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1021 | TSPP_DEBUG("tspp_open_stream %i %i %i %i", dev, channel_id, src, mode); |
| 1022 | if (dev >= TSPP_MAX_DEVICES) { |
| 1023 | pr_err("tspp: device id out of range"); |
| 1024 | return -ENODEV; |
| 1025 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1026 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1027 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1028 | pr_err("tspp: channel id out of range"); |
| 1029 | return -ECHRNG; |
| 1030 | } |
| 1031 | |
| 1032 | pdev = tspp_find_by_id(dev); |
| 1033 | if (!pdev) { |
| 1034 | pr_err("tspp_str: can't find device %i", dev); |
| 1035 | return -ENODEV; |
| 1036 | } |
| 1037 | channel = &pdev->channels[channel_id]; |
| 1038 | channel->src = src; |
| 1039 | tspp_set_tsif_mode(channel, mode); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1040 | |
| 1041 | switch (src) { |
| 1042 | case TSPP_SOURCE_TSIF0: |
| 1043 | /* make sure TSIF0 is running & enabled */ |
| 1044 | if (tspp_start_tsif(&pdev->tsif[0]) != 0) { |
| 1045 | pr_err("tspp: error starting tsif0"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1046 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1047 | } |
| 1048 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1049 | writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS, |
| 1050 | pdev->base + TSPP_CONTROL); |
| 1051 | wmb(); |
| 1052 | break; |
| 1053 | case TSPP_SOURCE_TSIF1: |
| 1054 | /* make sure TSIF1 is running & enabled */ |
| 1055 | if (tspp_start_tsif(&pdev->tsif[1]) != 0) { |
| 1056 | pr_err("tspp: error starting tsif1"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1057 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1058 | } |
| 1059 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1060 | writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS, |
| 1061 | pdev->base + TSPP_CONTROL); |
| 1062 | wmb(); |
| 1063 | break; |
| 1064 | case TSPP_SOURCE_MEM: |
| 1065 | break; |
| 1066 | default: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1067 | pr_err("tspp: channel %i invalid source %i", channel->id, src); |
| 1068 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
| 1073 | EXPORT_SYMBOL(tspp_open_stream); |
| 1074 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1075 | int tspp_close_stream(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1076 | { |
| 1077 | u32 val; |
| 1078 | struct tspp_device *pdev; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1079 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1080 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1081 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1082 | pr_err("tspp: channel id out of range"); |
| 1083 | return -ECHRNG; |
| 1084 | } |
| 1085 | pdev = tspp_find_by_id(dev); |
| 1086 | if (!pdev) { |
| 1087 | pr_err("tspp_cs: can't find device %i", dev); |
| 1088 | return -EBUSY; |
| 1089 | } |
| 1090 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1091 | |
| 1092 | switch (channel->src) { |
| 1093 | case TSPP_SOURCE_TSIF0: |
| 1094 | tspp_stop_tsif(&pdev->tsif[0]); |
| 1095 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1096 | writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS, |
| 1097 | pdev->base + TSPP_CONTROL); |
| 1098 | wmb(); |
| 1099 | break; |
| 1100 | case TSPP_SOURCE_TSIF1: |
| 1101 | tspp_stop_tsif(&pdev->tsif[1]); |
| 1102 | val = readl_relaxed(pdev->base + TSPP_CONTROL); |
| 1103 | writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS, |
| 1104 | pdev->base + TSPP_CONTROL); |
| 1105 | break; |
| 1106 | case TSPP_SOURCE_MEM: |
| 1107 | break; |
| 1108 | case TSPP_SOURCE_NONE: |
| 1109 | break; |
| 1110 | } |
| 1111 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1112 | channel->src = TSPP_SOURCE_NONE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1113 | return 0; |
| 1114 | } |
| 1115 | EXPORT_SYMBOL(tspp_close_stream); |
| 1116 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1117 | int tspp_open_channel(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1118 | { |
| 1119 | int rc = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1120 | struct sps_connect *config; |
| 1121 | struct sps_register_event *event; |
| 1122 | struct tspp_channel *channel; |
| 1123 | struct tspp_device *pdev; |
| 1124 | |
| 1125 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1126 | pr_err("tspp: channel id out of range"); |
| 1127 | return -ECHRNG; |
| 1128 | } |
| 1129 | pdev = tspp_find_by_id(dev); |
| 1130 | if (!pdev) { |
| 1131 | pr_err("tspp_oc: can't find device %i", dev); |
| 1132 | return -ENODEV; |
| 1133 | } |
| 1134 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1135 | |
| 1136 | if (channel->used) { |
| 1137 | pr_err("tspp channel already in use"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1138 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1141 | config = &channel->config; |
| 1142 | event = &channel->event; |
| 1143 | |
| 1144 | /* start the clocks if needed */ |
| 1145 | tspp_clock_start(pdev); |
| 1146 | if (tspp_channels_in_use(pdev) == 0) |
| 1147 | wake_lock(&pdev->wake_lock); |
| 1148 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1149 | /* mark it as used */ |
| 1150 | channel->used = 1; |
| 1151 | |
| 1152 | /* start the bam */ |
| 1153 | channel->pipe = sps_alloc_endpoint(); |
| 1154 | if (channel->pipe == 0) { |
| 1155 | pr_err("tspp: error allocating endpoint"); |
| 1156 | rc = -ENOMEM; |
| 1157 | goto err_sps_alloc; |
| 1158 | } |
| 1159 | |
| 1160 | /* get default configuration */ |
| 1161 | sps_get_config(channel->pipe, config); |
| 1162 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1163 | config->source = pdev->bam_handle; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1164 | config->destination = SPS_DEV_HANDLE_MEM; |
| 1165 | config->mode = SPS_MODE_SRC; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1166 | config->options = |
| 1167 | SPS_O_AUTO_ENABLE | /* connection is auto-enabled */ |
| 1168 | SPS_O_STREAMING | /* streaming mode */ |
| 1169 | SPS_O_DESC_DONE | /* interrupt on end of descriptor */ |
| 1170 | SPS_O_ACK_TRANSFERS; /* must use sps_get_iovec() */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1171 | config->src_pipe_index = channel->id; |
| 1172 | config->desc.size = |
| 1173 | (TSPP_SPS_DESCRIPTOR_COUNT + 1) * SPS_DESCRIPTOR_SIZE; |
| 1174 | config->desc.base = dma_alloc_coherent(NULL, |
| 1175 | config->desc.size, |
| 1176 | &config->desc.phys_base, |
| 1177 | GFP_KERNEL); |
| 1178 | if (config->desc.base == 0) { |
| 1179 | pr_err("tspp: error allocating sps descriptors"); |
| 1180 | rc = -ENOMEM; |
| 1181 | goto err_desc_alloc; |
| 1182 | } |
| 1183 | |
| 1184 | memset(config->desc.base, 0, config->desc.size); |
| 1185 | |
| 1186 | rc = sps_connect(channel->pipe, config); |
| 1187 | if (rc) { |
| 1188 | pr_err("tspp: error connecting bam"); |
| 1189 | goto err_connect; |
| 1190 | } |
| 1191 | |
| 1192 | event->mode = SPS_TRIGGER_CALLBACK; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1193 | event->options = SPS_O_DESC_DONE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1194 | event->callback = tspp_sps_complete_cb; |
| 1195 | event->xfer_done = NULL; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1196 | event->user = pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1197 | |
| 1198 | rc = sps_register_event(channel->pipe, event); |
| 1199 | if (rc) { |
| 1200 | pr_err("tspp: error registering event"); |
| 1201 | goto err_event; |
| 1202 | } |
| 1203 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1204 | rc = pm_runtime_get(&pdev->pdev->dev); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1205 | if (rc < 0) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1206 | dev_err(&pdev->pdev->dev, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1207 | "Runtime PM: Unable to wake up tspp device, rc = %d", |
| 1208 | rc); |
| 1209 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1210 | return 0; |
| 1211 | |
| 1212 | err_event: |
| 1213 | sps_disconnect(channel->pipe); |
| 1214 | err_connect: |
| 1215 | dma_free_coherent(NULL, config->desc.size, config->desc.base, |
| 1216 | config->desc.phys_base); |
| 1217 | err_desc_alloc: |
| 1218 | sps_free_endpoint(channel->pipe); |
| 1219 | err_sps_alloc: |
| 1220 | return rc; |
| 1221 | } |
| 1222 | EXPORT_SYMBOL(tspp_open_channel); |
| 1223 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1224 | int tspp_close_channel(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1225 | { |
| 1226 | int i; |
| 1227 | int id; |
| 1228 | u32 val; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1229 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1230 | struct sps_connect *config; |
| 1231 | struct tspp_device *pdev; |
| 1232 | struct tspp_channel *channel; |
| 1233 | struct tspp_mem_buffer *pbuf, *temp; |
| 1234 | |
| 1235 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1236 | pr_err("tspp: channel id out of range"); |
| 1237 | return -ECHRNG; |
| 1238 | } |
| 1239 | pdev = tspp_find_by_id(dev); |
| 1240 | if (!pdev) { |
| 1241 | pr_err("tspp_close: can't find device %i", dev); |
| 1242 | return -ENODEV; |
| 1243 | } |
| 1244 | channel = &pdev->channels[channel_id]; |
| 1245 | |
| 1246 | /* if the channel is not used, we are done */ |
| 1247 | if (!channel->used) |
| 1248 | return 0; |
| 1249 | |
| 1250 | channel->notifier = NULL; |
| 1251 | channel->notify_data = NULL; |
| 1252 | channel->notify_timer = 0; |
| 1253 | |
| 1254 | config = &channel->config; |
| 1255 | pdev = channel->pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1256 | |
| 1257 | /* disable pipe (channel) */ |
| 1258 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1259 | writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE); |
| 1260 | wmb(); |
| 1261 | |
| 1262 | /* unregister all filters for this channel */ |
| 1263 | for (i = 0; i < TSPP_NUM_PRIORITIES; i++) { |
| 1264 | struct tspp_pid_filter *tspp_filter = |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1265 | &pdev->filters[channel->src]->filter[i]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1266 | id = FILTER_GET_PIPE_NUMBER0(tspp_filter); |
| 1267 | if (id == channel->id) { |
| 1268 | if (FILTER_HAS_ENCRYPTION(tspp_filter)) |
| 1269 | tspp_free_key_entry( |
| 1270 | FILTER_GET_KEY_NUMBER(tspp_filter)); |
| 1271 | tspp_filter->config = 0; |
| 1272 | tspp_filter->filter = 0; |
| 1273 | } |
| 1274 | } |
| 1275 | channel->filter_count = 0; |
| 1276 | |
| 1277 | /* stop the stream */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1278 | tspp_close_stream(dev, channel->id); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1279 | |
| 1280 | /* disconnect the bam */ |
| 1281 | if (sps_disconnect(channel->pipe) != 0) |
| 1282 | pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id); |
| 1283 | |
| 1284 | /* destroy the buffers */ |
| 1285 | dma_free_coherent(NULL, config->desc.size, config->desc.base, |
| 1286 | config->desc.phys_base); |
| 1287 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1288 | pbuf = channel->data; |
| 1289 | for (i = 0; i < channel->buffer_count; i++) { |
| 1290 | if (pbuf->desc.phys_base) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1291 | dma_free_coherent(NULL, |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1292 | pbuf->desc.size, |
| 1293 | pbuf->desc.virt_base, |
| 1294 | pbuf->desc.phys_base); |
| 1295 | pbuf->desc.phys_base = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1296 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1297 | pbuf->desc.virt_base = 0; |
| 1298 | pbuf->state = TSPP_BUF_STATE_EMPTY; |
| 1299 | temp = pbuf; |
| 1300 | pbuf = pbuf->next; |
| 1301 | kfree(temp); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1302 | } |
| 1303 | channel->buffer_count = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1304 | channel->data = NULL; |
| 1305 | channel->read = NULL; |
| 1306 | channel->waiting = NULL; |
| 1307 | channel->locked = NULL; |
| 1308 | channel->used = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1309 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1310 | if (tspp_channels_in_use(pdev) == 0) |
| 1311 | wake_unlock(&pdev->wake_lock); |
| 1312 | tspp_clock_stop(pdev); |
| 1313 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | EXPORT_SYMBOL(tspp_close_channel); |
| 1317 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1318 | int tspp_add_filter(u32 dev, u32 channel_id, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1319 | struct tspp_filter *filter) |
| 1320 | { |
| 1321 | int i; |
| 1322 | int other_channel; |
| 1323 | int entry; |
| 1324 | u32 val, pid, enabled; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1325 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1326 | struct tspp_pid_filter p; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1327 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1328 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1329 | TSPP_DEBUG("tspp: add filter"); |
| 1330 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1331 | pr_err("tspp: channel id out of range"); |
| 1332 | return -ECHRNG; |
| 1333 | } |
| 1334 | pdev = tspp_find_by_id(dev); |
| 1335 | if (!pdev) { |
| 1336 | pr_err("tspp_add: can't find device %i", dev); |
| 1337 | return -ENODEV; |
| 1338 | } |
| 1339 | |
| 1340 | channel = &pdev->channels[channel_id]; |
| 1341 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1342 | if (filter->source > TSPP_SOURCE_MEM) { |
| 1343 | pr_err("tspp invalid source"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1344 | return -ENOSR; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | if (filter->priority >= TSPP_NUM_PRIORITIES) { |
| 1348 | pr_err("tspp invalid source"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1349 | return -ENOSR; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | /* make sure this filter mode matches the channel mode */ |
| 1353 | switch (channel->mode) { |
| 1354 | case TSPP_MODE_DISABLED: |
| 1355 | channel->mode = filter->mode; |
| 1356 | break; |
| 1357 | case TSPP_MODE_RAW: |
| 1358 | case TSPP_MODE_PES: |
| 1359 | case TSPP_MODE_RAW_NO_SUFFIX: |
| 1360 | if (filter->mode != channel->mode) { |
| 1361 | pr_err("tspp: wrong filter mode"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1362 | return -EBADSLT; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | if (filter->mode == TSPP_MODE_PES) { |
| 1367 | for (i = 0; i < TSPP_NUM_PRIORITIES; i++) { |
| 1368 | struct tspp_pid_filter *tspp_filter = |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1369 | &pdev->filters[channel->src]->filter[i]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1370 | pid = FILTER_GET_PIPE_PID((tspp_filter)); |
| 1371 | enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter); |
| 1372 | if (enabled && (pid == filter->pid)) { |
| 1373 | other_channel = |
| 1374 | FILTER_GET_PIPE_NUMBER0(tspp_filter); |
| 1375 | pr_err("tspp: pid 0x%x already in use by channel %i", |
| 1376 | filter->pid, other_channel); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1377 | return -EBADSLT; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | /* make sure this priority is not already in use */ |
| 1383 | enabled = FILTER_GET_PIPE_PROCESS0( |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1384 | (&(pdev->filters[channel->src]->filter[filter->priority]))); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1385 | if (enabled) { |
| 1386 | pr_err("tspp: filter priority %i source %i is already enabled\n", |
| 1387 | filter->priority, channel->src); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1388 | return -ENOSR; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | if (channel->mode == TSPP_MODE_PES) { |
| 1392 | /* if we are already processing in PES mode, disable pipe |
| 1393 | (channel) and filter to be updated */ |
| 1394 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1395 | writel_relaxed(val | (1 << channel->id), |
| 1396 | pdev->base + TSPP_PS_DISABLE); |
| 1397 | wmb(); |
| 1398 | } |
| 1399 | |
| 1400 | /* update entry */ |
| 1401 | p.filter = 0; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1402 | p.config = FILTER_TRANS_END_DISABLE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1403 | FILTER_SET_PIPE_PROCESS0((&p), filter->mode); |
| 1404 | FILTER_SET_PIPE_PID((&p), filter->pid); |
| 1405 | FILTER_SET_PID_MASK((&p), filter->mask); |
| 1406 | FILTER_SET_PIPE_NUMBER0((&p), channel->id); |
| 1407 | FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED); |
| 1408 | if (filter->decrypt) { |
| 1409 | entry = tspp_get_key_entry(); |
| 1410 | if (entry == -1) { |
| 1411 | pr_err("tspp: no more keys available!"); |
| 1412 | } else { |
| 1413 | p.config |= FILTER_DECRYPT; |
| 1414 | FILTER_SET_KEY_NUMBER((&p), entry); |
| 1415 | } |
| 1416 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1417 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1418 | pdev->filters[channel->src]-> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1419 | filter[filter->priority].config = p.config; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1420 | pdev->filters[channel->src]-> |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1421 | filter[filter->priority].filter = p.filter; |
| 1422 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1423 | /* allocate buffers if needed */ |
| 1424 | tspp_allocate_buffers(dev, channel->id, channel->max_buffers, |
| 1425 | channel->buffer_size, channel->int_freq, 0, 0); |
| 1426 | if (channel->buffer_count < MIN_ACCEPTABLE_BUFFER_COUNT) { |
| 1427 | pr_err("tspp: failed to allocate at least %i buffers", |
| 1428 | MIN_ACCEPTABLE_BUFFER_COUNT); |
| 1429 | return -ENOMEM; |
| 1430 | } |
| 1431 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1432 | /* reenable pipe */ |
| 1433 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1434 | writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE); |
| 1435 | wmb(); |
| 1436 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1437 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1438 | channel->filter_count++; |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | EXPORT_SYMBOL(tspp_add_filter); |
| 1443 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1444 | int tspp_remove_filter(u32 dev, u32 channel_id, |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1445 | struct tspp_filter *filter) |
| 1446 | { |
| 1447 | int entry; |
| 1448 | u32 val; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1449 | struct tspp_device *pdev; |
| 1450 | int src; |
| 1451 | struct tspp_pid_filter *tspp_filter; |
| 1452 | struct tspp_channel *channel; |
| 1453 | |
| 1454 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1455 | pr_err("tspp: channel id out of range"); |
| 1456 | return -ECHRNG; |
| 1457 | } |
| 1458 | pdev = tspp_find_by_id(dev); |
| 1459 | if (!pdev) { |
| 1460 | pr_err("tspp_remove: can't find device %i", dev); |
| 1461 | return -ENODEV; |
| 1462 | } |
| 1463 | channel = &pdev->channels[channel_id]; |
| 1464 | |
| 1465 | src = channel->src; |
| 1466 | tspp_filter = &(pdev->filters[src]->filter[filter->priority]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1467 | |
| 1468 | /* disable pipe (channel) */ |
| 1469 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1470 | writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE); |
| 1471 | wmb(); |
| 1472 | |
| 1473 | /* update data keys */ |
| 1474 | if (tspp_filter->config & FILTER_DECRYPT) { |
| 1475 | entry = FILTER_GET_KEY_NUMBER(tspp_filter); |
| 1476 | tspp_free_key_entry(entry); |
| 1477 | } |
| 1478 | |
| 1479 | /* update pid table */ |
| 1480 | tspp_filter->config = 0; |
| 1481 | tspp_filter->filter = 0; |
| 1482 | |
| 1483 | channel->filter_count--; |
| 1484 | |
| 1485 | /* reenable pipe */ |
| 1486 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1487 | writel_relaxed(val & ~(1 << channel->id), |
| 1488 | pdev->base + TSPP_PS_DISABLE); |
| 1489 | wmb(); |
| 1490 | val = readl_relaxed(pdev->base + TSPP_PS_DISABLE); |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | EXPORT_SYMBOL(tspp_remove_filter); |
| 1495 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1496 | 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] | 1497 | { |
| 1498 | int i; |
| 1499 | int id; |
| 1500 | int key_index; |
| 1501 | int data; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1502 | struct tspp_channel *channel; |
| 1503 | struct tspp_device *pdev; |
| 1504 | |
| 1505 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1506 | pr_err("tspp: channel id out of range"); |
| 1507 | return -ECHRNG; |
| 1508 | } |
| 1509 | pdev = tspp_find_by_id(dev); |
| 1510 | if (!pdev) { |
| 1511 | pr_err("tspp_set: can't find device %i", dev); |
| 1512 | return -ENODEV; |
| 1513 | } |
| 1514 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1515 | |
| 1516 | /* read the key index used by this channel */ |
| 1517 | for (i = 0; i < TSPP_NUM_PRIORITIES; i++) { |
| 1518 | struct tspp_pid_filter *tspp_filter = |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1519 | &(pdev->filters[channel->src]->filter[i]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1520 | id = FILTER_GET_PIPE_NUMBER0(tspp_filter); |
| 1521 | if (id == channel->id) { |
| 1522 | if (FILTER_HAS_ENCRYPTION(tspp_filter)) { |
| 1523 | key_index = FILTER_GET_KEY_NUMBER(tspp_filter); |
| 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | if (i == TSPP_NUM_PRIORITIES) { |
| 1529 | pr_err("tspp: no encryption on this channel"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1530 | return -ENOKEY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | if (key->parity == TSPP_KEY_PARITY_EVEN) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1534 | pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb; |
| 1535 | pdev->tspp_key_table->entry[key_index].even_msb = key->msb; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1536 | } else { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1537 | pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb; |
| 1538 | pdev->tspp_key_table->entry[key_index].odd_msb = key->msb; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1539 | } |
| 1540 | data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID); |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | EXPORT_SYMBOL(tspp_set_key); |
| 1545 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1546 | int tspp_register_notification(u32 dev, u32 channel_id, |
| 1547 | tspp_notifier *pNotify, void *userdata, u32 timer_ms) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1548 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1549 | struct tspp_channel *channel; |
| 1550 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1551 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1552 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1553 | pr_err("tspp: channel id out of range"); |
| 1554 | return -ECHRNG; |
| 1555 | } |
| 1556 | pdev = tspp_find_by_id(dev); |
| 1557 | if (!pdev) { |
| 1558 | pr_err("tspp_reg: can't find device %i", dev); |
| 1559 | return -ENODEV; |
| 1560 | } |
| 1561 | channel = &pdev->channels[channel_id]; |
| 1562 | channel->notifier = pNotify; |
| 1563 | channel->notify_data = userdata; |
| 1564 | channel->notify_timer = timer_ms; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1565 | return 0; |
| 1566 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1567 | EXPORT_SYMBOL(tspp_register_notification); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1568 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1569 | int tspp_unregister_notification(u32 dev, u32 channel_id) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1570 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1571 | struct tspp_channel *channel; |
| 1572 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1573 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1574 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1575 | pr_err("tspp: channel id out of range"); |
| 1576 | return -ECHRNG; |
| 1577 | } |
| 1578 | pdev = tspp_find_by_id(dev); |
| 1579 | if (!pdev) { |
| 1580 | pr_err("tspp_unreg: can't find device %i", dev); |
| 1581 | return -ENODEV; |
| 1582 | } |
| 1583 | channel = &pdev->channels[channel_id]; |
| 1584 | channel->notifier = NULL; |
| 1585 | channel->notify_data = 0; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1586 | return 0; |
| 1587 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1588 | EXPORT_SYMBOL(tspp_unregister_notification); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1589 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1590 | 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] | 1591 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1592 | struct tspp_mem_buffer *buffer; |
| 1593 | struct tspp_channel *channel; |
| 1594 | struct tspp_device *pdev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1595 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1596 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1597 | pr_err("tspp: channel id out of range"); |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | pdev = tspp_find_by_id(dev); |
| 1601 | if (!pdev) { |
| 1602 | pr_err("tspp_get: can't find device %i", dev); |
| 1603 | return NULL; |
| 1604 | } |
| 1605 | channel = &pdev->channels[channel_id]; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1606 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1607 | if (!channel->read) { |
| 1608 | pr_warn("tspp: no buffer to get on channel %i!", |
| 1609 | channel->id); |
| 1610 | return NULL; |
| 1611 | } |
| 1612 | |
| 1613 | buffer = channel->read; |
| 1614 | /* see if we have any buffers ready to read */ |
| 1615 | if (buffer->state != TSPP_BUF_STATE_DATA) |
| 1616 | return 0; |
| 1617 | |
| 1618 | if (buffer->state == TSPP_BUF_STATE_DATA) { |
| 1619 | /* mark the buffer as busy */ |
| 1620 | buffer->state = TSPP_BUF_STATE_LOCKED; |
| 1621 | |
| 1622 | /* increment the pointer along the list */ |
| 1623 | channel->read = channel->read->next; |
| 1624 | } |
| 1625 | |
| 1626 | return &buffer->desc; |
| 1627 | } |
| 1628 | EXPORT_SYMBOL(tspp_get_buffer); |
| 1629 | |
| 1630 | int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id) |
| 1631 | { |
| 1632 | int i, found = 0; |
| 1633 | struct tspp_mem_buffer *buffer; |
| 1634 | struct tspp_channel *channel; |
| 1635 | struct tspp_device *pdev; |
| 1636 | |
| 1637 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1638 | pr_err("tspp: channel id out of range"); |
| 1639 | return -ECHRNG; |
| 1640 | } |
| 1641 | pdev = tspp_find_by_id(dev); |
| 1642 | if (!pdev) { |
| 1643 | pr_err("tspp: can't find device %i", dev); |
| 1644 | return -ENODEV; |
| 1645 | } |
| 1646 | channel = &pdev->channels[channel_id]; |
| 1647 | |
| 1648 | if (descriptor_id > channel->buffer_count) |
| 1649 | pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id); |
| 1650 | |
| 1651 | /* find the correct descriptor */ |
| 1652 | buffer = channel->locked; |
| 1653 | for (i = 0; i < channel->buffer_count; i++) { |
| 1654 | if (buffer->desc.id == descriptor_id) { |
| 1655 | found = 1; |
| 1656 | break; |
| 1657 | } |
| 1658 | buffer = buffer->next; |
| 1659 | } |
| 1660 | channel->locked = channel->locked->next; |
| 1661 | |
| 1662 | if (!found) { |
| 1663 | pr_err("tspp: cant find desc %i", descriptor_id); |
| 1664 | return -EINVAL; |
| 1665 | } |
| 1666 | |
| 1667 | /* make sure the buffer is in the expected state */ |
| 1668 | if (buffer->state != TSPP_BUF_STATE_LOCKED) { |
| 1669 | pr_err("tspp: buffer %i not locked", descriptor_id); |
| 1670 | return -EINVAL; |
| 1671 | } |
| 1672 | /* unlock the buffer and requeue it */ |
| 1673 | buffer->state = TSPP_BUF_STATE_WAITING; |
| 1674 | |
| 1675 | if (tspp_queue_buffer(channel, buffer)) |
| 1676 | pr_warn("tspp: can't requeue buffer"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1677 | return 0; |
| 1678 | } |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1679 | EXPORT_SYMBOL(tspp_release_buffer); |
| 1680 | |
| 1681 | int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, |
| 1682 | u32 size, u32 int_freq, tspp_allocator *alloc, void *user) |
| 1683 | { |
| 1684 | struct tspp_channel *channel; |
| 1685 | struct tspp_device *pdev; |
| 1686 | struct tspp_mem_buffer *last = NULL; |
| 1687 | |
| 1688 | TSPP_DEBUG("tspp_allocate_buffers"); |
| 1689 | |
| 1690 | if (channel_id >= TSPP_NUM_CHANNELS) { |
| 1691 | pr_err("tspp: channel id out of range"); |
| 1692 | return -ECHRNG; |
| 1693 | } |
| 1694 | pdev = tspp_find_by_id(dev); |
| 1695 | if (!pdev) { |
| 1696 | pr_err("tspp_alloc: can't find device %i", dev); |
| 1697 | return -ENODEV; |
| 1698 | } |
| 1699 | channel = &pdev->channels[channel_id]; |
| 1700 | |
| 1701 | channel->max_buffers = count; |
| 1702 | |
| 1703 | /* set up interrupt frequency */ |
| 1704 | if (int_freq > channel->max_buffers) |
| 1705 | int_freq = channel->max_buffers; |
| 1706 | channel->int_freq = int_freq; |
| 1707 | |
| 1708 | switch (channel->mode) { |
| 1709 | case TSPP_MODE_DISABLED: |
| 1710 | case TSPP_MODE_PES: |
| 1711 | /* give the user what he asks for */ |
| 1712 | channel->buffer_size = size; |
| 1713 | break; |
| 1714 | |
| 1715 | case TSPP_MODE_RAW: |
| 1716 | /* must be a multiple of 192 */ |
| 1717 | if (size < (TSPP_PACKET_LENGTH+4)) |
| 1718 | channel->buffer_size = (TSPP_PACKET_LENGTH+4); |
| 1719 | else |
| 1720 | channel->buffer_size = (size / |
| 1721 | (TSPP_PACKET_LENGTH+4)) * |
| 1722 | (TSPP_PACKET_LENGTH+4); |
| 1723 | break; |
| 1724 | |
| 1725 | case TSPP_MODE_RAW_NO_SUFFIX: |
| 1726 | /* must be a multiple of 188 */ |
| 1727 | channel->buffer_size = (size / TSPP_PACKET_LENGTH) * |
| 1728 | TSPP_PACKET_LENGTH; |
| 1729 | break; |
| 1730 | } |
| 1731 | |
| 1732 | for (; channel->buffer_count < channel->max_buffers; |
| 1733 | channel->buffer_count++) { |
| 1734 | |
| 1735 | /* allocate the descriptor */ |
| 1736 | struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *) |
| 1737 | kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL); |
| 1738 | if (!desc) { |
| 1739 | pr_warn("tspp: Can't allocate desc %i", |
| 1740 | channel->buffer_count); |
| 1741 | break; |
| 1742 | } |
| 1743 | |
| 1744 | desc->desc.id = channel->buffer_count; |
| 1745 | /* allocate the buffer */ |
| 1746 | if (tspp_alloc_buffer(channel_id, &desc->desc, |
| 1747 | channel->buffer_size, alloc, user) != 0) { |
| 1748 | kfree(desc); |
| 1749 | pr_warn("tspp: Can't allocate buffer %i", |
| 1750 | channel->buffer_count); |
| 1751 | break; |
| 1752 | } |
| 1753 | |
| 1754 | /* add the descriptor to the list */ |
| 1755 | desc->filled = 0; |
| 1756 | desc->read_index = 0; |
| 1757 | if (!channel->data) { |
| 1758 | channel->data = desc; |
| 1759 | desc->next = channel->data; |
| 1760 | } else { |
| 1761 | last->next = desc; |
| 1762 | } |
| 1763 | last = desc; |
| 1764 | desc->next = channel->data; |
| 1765 | |
| 1766 | /* prepare the sps descriptor */ |
| 1767 | desc->sps.phys_base = desc->desc.phys_base; |
| 1768 | desc->sps.base = desc->desc.virt_base; |
| 1769 | desc->sps.size = desc->desc.size; |
| 1770 | |
| 1771 | /* start the transfer */ |
| 1772 | if (tspp_queue_buffer(channel, desc)) |
| 1773 | pr_err("tspp: can't queue buffer %i", desc->desc.id); |
| 1774 | } |
| 1775 | |
| 1776 | channel->waiting = channel->data; |
| 1777 | channel->read = channel->data; |
| 1778 | channel->locked = channel->data; |
| 1779 | return 0; |
| 1780 | } |
| 1781 | EXPORT_SYMBOL(tspp_allocate_buffers); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1782 | |
| 1783 | /*** File Operations ***/ |
| 1784 | static ssize_t tspp_open(struct inode *inode, struct file *filp) |
| 1785 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1786 | u32 dev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1787 | struct tspp_channel *channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1788 | |
| 1789 | TSPP_DEBUG("tspp_open"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1790 | channel = container_of(inode->i_cdev, struct tspp_channel, cdev); |
| 1791 | filp->private_data = channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1792 | dev = channel->pdev->pdev->id; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1793 | |
| 1794 | /* if this channel is already in use, quit */ |
| 1795 | if (channel->used) { |
| 1796 | pr_err("tspp channel %i already in use", |
| 1797 | MINOR(channel->cdev.dev)); |
| 1798 | return -EACCES; |
| 1799 | } |
| 1800 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1801 | if (tspp_open_channel(dev, channel->id) != 0) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1802 | pr_err("tspp: error opening channel"); |
| 1803 | return -EACCES; |
| 1804 | } |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p) |
| 1810 | { |
| 1811 | unsigned long flags; |
| 1812 | unsigned int mask = 0; |
| 1813 | struct tspp_channel *channel; |
| 1814 | channel = filp->private_data; |
| 1815 | |
| 1816 | /* register the wait queue for this channel */ |
| 1817 | poll_wait(filp, &channel->in_queue, p); |
| 1818 | |
| 1819 | spin_lock_irqsave(&channel->pdev->spinlock, flags); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1820 | if (channel->read && |
| 1821 | channel->read->state == TSPP_BUF_STATE_DATA) |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1822 | mask = POLLIN | POLLRDNORM; |
| 1823 | |
| 1824 | spin_unlock_irqrestore(&channel->pdev->spinlock, flags); |
| 1825 | |
| 1826 | return mask; |
| 1827 | } |
| 1828 | |
| 1829 | static ssize_t tspp_release(struct inode *inode, struct file *filp) |
| 1830 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1831 | struct tspp_channel *channel = filp->private_data; |
| 1832 | u32 dev = channel->pdev->pdev->id; |
| 1833 | TSPP_DEBUG("tspp_release"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1834 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1835 | tspp_close_channel(dev, channel->id); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
| 1840 | static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count, |
| 1841 | loff_t *f_pos) |
| 1842 | { |
| 1843 | size_t size = 0; |
| 1844 | size_t transferred = 0; |
| 1845 | struct tspp_channel *channel; |
| 1846 | struct tspp_mem_buffer *buffer; |
| 1847 | channel = filp->private_data; |
| 1848 | |
| 1849 | TSPP_DEBUG("tspp_read"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1850 | |
| 1851 | while (!channel->read) { |
| 1852 | if (filp->f_flags & O_NONBLOCK) { |
| 1853 | pr_warn("tspp: no buffer on channel %i!", |
| 1854 | channel->id); |
| 1855 | return -EAGAIN; |
| 1856 | } |
| 1857 | /* go to sleep if there is nothing to read */ |
| 1858 | if (wait_event_interruptible(channel->in_queue, |
| 1859 | (channel->read != NULL))) { |
| 1860 | pr_err("tspp: rude awakening\n"); |
| 1861 | return -ERESTARTSYS; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | buffer = channel->read; |
| 1866 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1867 | /* see if we have any buffers ready to read */ |
| 1868 | while (buffer->state != TSPP_BUF_STATE_DATA) { |
| 1869 | if (filp->f_flags & O_NONBLOCK) { |
| 1870 | pr_warn("tspp: nothing to read on channel %i!", |
| 1871 | channel->id); |
| 1872 | return -EAGAIN; |
| 1873 | } |
| 1874 | /* go to sleep if there is nothing to read */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1875 | if (wait_event_interruptible(channel->in_queue, |
| 1876 | (buffer->state == TSPP_BUF_STATE_DATA))) { |
| 1877 | pr_err("tspp: rude awakening\n"); |
| 1878 | return -ERESTARTSYS; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | while (buffer->state == TSPP_BUF_STATE_DATA) { |
| 1883 | size = min(count, buffer->filled); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1884 | if (size == 0) |
| 1885 | break; |
| 1886 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1887 | if (copy_to_user(buf, buffer->desc.virt_base + |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1888 | buffer->read_index, size)) { |
| 1889 | pr_err("tspp: error copying to user buffer"); |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1890 | return -EBUSY; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1891 | } |
| 1892 | buf += size; |
| 1893 | count -= size; |
| 1894 | transferred += size; |
| 1895 | buffer->read_index += size; |
| 1896 | |
| 1897 | /* after reading the end of the buffer, requeue it, |
| 1898 | and set up for reading the next one */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1899 | if (buffer->read_index == buffer->filled) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1900 | buffer->state = TSPP_BUF_STATE_WAITING; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1901 | if (tspp_queue_buffer(channel, buffer)) |
| 1902 | pr_err("tspp: can't submit transfer"); |
| 1903 | channel->locked = channel->read; |
| 1904 | channel->read = channel->read->next; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | return transferred; |
| 1909 | } |
| 1910 | |
| 1911 | static long tspp_ioctl(struct file *filp, |
| 1912 | unsigned int param0, unsigned long param1) |
| 1913 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1914 | u32 dev; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1915 | int rc = -1; |
| 1916 | struct tspp_channel *channel; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1917 | struct tspp_select_source ss; |
| 1918 | struct tspp_filter f; |
| 1919 | struct tspp_key k; |
| 1920 | struct tspp_iv iv; |
| 1921 | struct tspp_system_keys sk; |
| 1922 | struct tspp_buffer b; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1923 | channel = filp->private_data; |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1924 | dev = channel->pdev->pdev->id; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1925 | |
| 1926 | if (!param1) |
| 1927 | return -EINVAL; |
| 1928 | |
| 1929 | switch (param0) { |
| 1930 | case TSPP_IOCTL_SELECT_SOURCE: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1931 | if (!access_ok(VERIFY_READ, param1, |
| 1932 | sizeof(struct tspp_select_source))) { |
| 1933 | return -EBUSY; |
| 1934 | } |
| 1935 | if (__copy_from_user(&ss, (void *)param1, |
| 1936 | sizeof(struct tspp_select_source)) == 0) |
| 1937 | rc = tspp_select_source(dev, channel->id, &ss); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1938 | break; |
| 1939 | case TSPP_IOCTL_ADD_FILTER: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1940 | if (!access_ok(VERIFY_READ, param1, |
| 1941 | sizeof(struct tspp_filter))) { |
| 1942 | return -ENOSR; |
| 1943 | } |
| 1944 | if (__copy_from_user(&f, (void *)param1, |
| 1945 | sizeof(struct tspp_filter)) == 0) |
| 1946 | rc = tspp_add_filter(dev, channel->id, &f); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1947 | break; |
| 1948 | case TSPP_IOCTL_REMOVE_FILTER: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1949 | if (!access_ok(VERIFY_READ, param1, |
| 1950 | sizeof(struct tspp_filter))) { |
| 1951 | return -EBUSY; |
| 1952 | } |
| 1953 | if (__copy_from_user(&f, (void *)param1, |
| 1954 | sizeof(struct tspp_filter)) == 0) |
| 1955 | rc = tspp_remove_filter(dev, channel->id, &f); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1956 | break; |
| 1957 | case TSPP_IOCTL_SET_KEY: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1958 | if (!access_ok(VERIFY_READ, param1, |
| 1959 | sizeof(struct tspp_key))) { |
| 1960 | return -EBUSY; |
| 1961 | } |
| 1962 | if (__copy_from_user(&k, (void *)param1, |
| 1963 | sizeof(struct tspp_key)) == 0) |
| 1964 | rc = tspp_set_key(dev, channel->id, &k); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1965 | break; |
| 1966 | case TSPP_IOCTL_SET_IV: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1967 | if (!access_ok(VERIFY_READ, param1, |
| 1968 | sizeof(struct tspp_iv))) { |
| 1969 | return -EBUSY; |
| 1970 | } |
| 1971 | if (__copy_from_user(&iv, (void *)param1, |
| 1972 | sizeof(struct tspp_iv)) == 0) |
| 1973 | rc = tspp_set_iv(channel, &iv); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1974 | break; |
| 1975 | case TSPP_IOCTL_SET_SYSTEM_KEYS: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1976 | if (!access_ok(VERIFY_READ, param1, |
| 1977 | sizeof(struct tspp_system_keys))) { |
| 1978 | return -EINVAL; |
| 1979 | } |
| 1980 | if (__copy_from_user(&sk, (void *)param1, |
| 1981 | sizeof(struct tspp_system_keys)) == 0) |
| 1982 | rc = tspp_set_system_keys(channel, &sk); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1983 | break; |
| 1984 | case TSPP_IOCTL_BUFFER_SIZE: |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 1985 | if (!access_ok(VERIFY_READ, param1, |
| 1986 | sizeof(struct tspp_buffer))) { |
| 1987 | rc = -EINVAL; |
| 1988 | } |
| 1989 | if (__copy_from_user(&b, (void *)param1, |
| 1990 | sizeof(struct tspp_buffer)) == 0) |
| 1991 | rc = tspp_set_buffer_size(channel, &b); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 1992 | break; |
| 1993 | default: |
| 1994 | pr_err("tspp: Unknown ioctl %i", param0); |
| 1995 | } |
| 1996 | |
| 1997 | /* normalize the return code in case one of the subfunctions does |
| 1998 | something weird */ |
| 1999 | if (rc != 0) |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2000 | rc = -ENOIOCTLCMD; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2001 | |
| 2002 | return rc; |
| 2003 | } |
| 2004 | |
| 2005 | /*** debugfs ***/ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2006 | static int debugfs_iomem_x32_set(void *data, u64 val) |
| 2007 | { |
| 2008 | writel_relaxed(val, data); |
| 2009 | wmb(); |
| 2010 | return 0; |
| 2011 | } |
| 2012 | |
| 2013 | static int debugfs_iomem_x32_get(void *data, u64 *val) |
| 2014 | { |
| 2015 | *val = readl_relaxed(data); |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get, |
| 2020 | debugfs_iomem_x32_set, "0x%08llx"); |
| 2021 | |
| 2022 | static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device, |
| 2023 | int instance) |
| 2024 | { |
| 2025 | char name[10]; |
| 2026 | snprintf(name, 10, "tsif%i", instance); |
| 2027 | tsif_device->dent_tsif = debugfs_create_dir( |
| 2028 | name, NULL); |
| 2029 | if (tsif_device->dent_tsif) { |
| 2030 | int i; |
| 2031 | void __iomem *base = tsif_device->base; |
| 2032 | for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) { |
| 2033 | tsif_device->debugfs_tsif_regs[i] = |
| 2034 | debugfs_create_file( |
| 2035 | debugfs_tsif_regs[i].name, |
| 2036 | debugfs_tsif_regs[i].mode, |
| 2037 | tsif_device->dent_tsif, |
| 2038 | base + debugfs_tsif_regs[i].offset, |
| 2039 | &fops_iomem_x32); |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device) |
| 2045 | { |
| 2046 | if (tsif_device->dent_tsif) { |
| 2047 | int i; |
| 2048 | debugfs_remove_recursive(tsif_device->dent_tsif); |
| 2049 | tsif_device->dent_tsif = NULL; |
| 2050 | for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) |
| 2051 | tsif_device->debugfs_tsif_regs[i] = NULL; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | static void tspp_debugfs_init(struct tspp_device *device, int instance) |
| 2056 | { |
| 2057 | char name[10]; |
| 2058 | snprintf(name, 10, "tspp%i", instance); |
| 2059 | device->dent = debugfs_create_dir( |
| 2060 | name, NULL); |
| 2061 | if (device->dent) { |
| 2062 | int i; |
| 2063 | void __iomem *base = device->base; |
| 2064 | for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) { |
| 2065 | device->debugfs_regs[i] = |
| 2066 | debugfs_create_file( |
| 2067 | debugfs_tspp_regs[i].name, |
| 2068 | debugfs_tspp_regs[i].mode, |
| 2069 | device->dent, |
| 2070 | base + debugfs_tspp_regs[i].offset, |
| 2071 | &fops_iomem_x32); |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | static void tspp_debugfs_exit(struct tspp_device *device) |
| 2077 | { |
| 2078 | if (device->dent) { |
| 2079 | int i; |
| 2080 | debugfs_remove_recursive(device->dent); |
| 2081 | device->dent = NULL; |
| 2082 | for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) |
| 2083 | device->debugfs_regs[i] = NULL; |
| 2084 | } |
| 2085 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2086 | |
| 2087 | static int __devinit msm_tspp_probe(struct platform_device *pdev) |
| 2088 | { |
| 2089 | int rc = -ENODEV; |
| 2090 | u32 version; |
| 2091 | u32 i; |
| 2092 | struct msm_tspp_platform_data *data; |
| 2093 | struct tspp_device *device; |
| 2094 | struct resource *mem_tsif0; |
| 2095 | struct resource *mem_tsif1; |
| 2096 | struct resource *mem_tspp; |
| 2097 | struct resource *mem_bam; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2098 | |
| 2099 | /* must have platform data */ |
| 2100 | data = pdev->dev.platform_data; |
| 2101 | if (!data) { |
| 2102 | pr_err("tspp: Platform data not available"); |
| 2103 | rc = -EINVAL; |
| 2104 | goto out; |
| 2105 | } |
| 2106 | |
| 2107 | /* check for valid device id */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2108 | if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2109 | pr_err("tspp: Invalid device ID %d", pdev->id); |
| 2110 | rc = -EINVAL; |
| 2111 | goto out; |
| 2112 | } |
| 2113 | |
| 2114 | /* OK, we will use this device */ |
| 2115 | device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL); |
| 2116 | if (!device) { |
| 2117 | pr_err("tspp: Failed to allocate memory for device"); |
| 2118 | rc = -ENOMEM; |
| 2119 | goto out; |
| 2120 | } |
| 2121 | |
| 2122 | /* set up references */ |
| 2123 | device->pdev = pdev; |
| 2124 | platform_set_drvdata(pdev, device); |
| 2125 | |
| 2126 | /* map clocks */ |
| 2127 | if (data->tsif_pclk) { |
Joel Nider | b9662ca | 2012-06-10 14:21:11 +0300 | [diff] [blame] | 2128 | device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2129 | if (IS_ERR(device->tsif_pclk)) { |
| 2130 | pr_err("tspp: failed to get %s", |
| 2131 | data->tsif_pclk); |
| 2132 | rc = PTR_ERR(device->tsif_pclk); |
| 2133 | device->tsif_pclk = NULL; |
| 2134 | goto err_pclock; |
| 2135 | } |
| 2136 | } |
| 2137 | if (data->tsif_ref_clk) { |
Joel Nider | b9662ca | 2012-06-10 14:21:11 +0300 | [diff] [blame] | 2138 | device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2139 | if (IS_ERR(device->tsif_ref_clk)) { |
| 2140 | pr_err("tspp: failed to get %s", |
| 2141 | data->tsif_ref_clk); |
| 2142 | rc = PTR_ERR(device->tsif_ref_clk); |
| 2143 | device->tsif_ref_clk = NULL; |
| 2144 | goto err_refclock; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /* map I/O memory */ |
| 2149 | mem_tsif0 = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2150 | if (!mem_tsif0) { |
| 2151 | pr_err("tspp: Missing tsif0 MEM resource"); |
| 2152 | rc = -ENXIO; |
| 2153 | goto err_res_tsif0; |
| 2154 | } |
| 2155 | device->tsif[0].base = ioremap(mem_tsif0->start, |
| 2156 | resource_size(mem_tsif0)); |
| 2157 | if (!device->tsif[0].base) { |
| 2158 | pr_err("tspp: ioremap failed"); |
| 2159 | goto err_map_tsif0; |
| 2160 | } |
| 2161 | |
| 2162 | mem_tsif1 = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 2163 | if (!mem_tsif1) { |
| 2164 | dev_err(&pdev->dev, "Missing tsif1 MEM resource"); |
| 2165 | rc = -ENXIO; |
| 2166 | goto err_res_tsif1; |
| 2167 | } |
| 2168 | device->tsif[1].base = ioremap(mem_tsif1->start, |
| 2169 | resource_size(mem_tsif1)); |
| 2170 | if (!device->tsif[1].base) { |
| 2171 | dev_err(&pdev->dev, "ioremap failed"); |
| 2172 | goto err_map_tsif1; |
| 2173 | } |
| 2174 | |
| 2175 | mem_tspp = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 2176 | if (!mem_tspp) { |
| 2177 | dev_err(&pdev->dev, "Missing MEM resource"); |
| 2178 | rc = -ENXIO; |
| 2179 | goto err_res_dev; |
| 2180 | } |
| 2181 | device->base = ioremap(mem_tspp->start, resource_size(mem_tspp)); |
| 2182 | if (!device->base) { |
| 2183 | dev_err(&pdev->dev, "ioremap failed"); |
| 2184 | goto err_map_dev; |
| 2185 | } |
| 2186 | |
| 2187 | mem_bam = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 2188 | if (!mem_bam) { |
| 2189 | pr_err("tspp: Missing bam MEM resource"); |
| 2190 | rc = -ENXIO; |
| 2191 | goto err_res_bam; |
| 2192 | } |
| 2193 | memset(&device->bam_props, 0, sizeof(device->bam_props)); |
| 2194 | device->bam_props.phys_addr = mem_bam->start; |
| 2195 | device->bam_props.virt_addr = ioremap(mem_bam->start, |
| 2196 | resource_size(mem_bam)); |
| 2197 | if (!device->bam_props.virt_addr) { |
| 2198 | dev_err(&pdev->dev, "ioremap failed"); |
| 2199 | goto err_map_bam; |
| 2200 | } |
| 2201 | |
| 2202 | /* map TSPP IRQ */ |
| 2203 | rc = platform_get_irq(pdev, 0); |
| 2204 | if (rc > 0) { |
| 2205 | device->tspp_irq = rc; |
| 2206 | rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED, |
| 2207 | dev_name(&pdev->dev), device); |
| 2208 | if (rc) { |
| 2209 | dev_err(&pdev->dev, "failed to request IRQ %d : %d", |
| 2210 | device->tspp_irq, rc); |
| 2211 | goto err_irq; |
| 2212 | } |
| 2213 | } else { |
| 2214 | dev_err(&pdev->dev, "failed to get tspp IRQ"); |
| 2215 | goto err_irq; |
| 2216 | } |
| 2217 | |
| 2218 | /* BAM IRQ */ |
| 2219 | device->bam_irq = TSIF_BAM_IRQ; |
| 2220 | |
| 2221 | /* GPIOs */ |
| 2222 | rc = tspp_start_gpios(device); |
| 2223 | if (rc) |
| 2224 | goto err_gpio; |
| 2225 | |
| 2226 | /* power management */ |
| 2227 | pm_runtime_set_active(&pdev->dev); |
| 2228 | pm_runtime_enable(&pdev->dev); |
| 2229 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2230 | tspp_debugfs_init(device, 0); |
| 2231 | |
| 2232 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 2233 | tsif_debugfs_init(&device->tsif[i], i); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2234 | |
| 2235 | wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND, |
| 2236 | dev_name(&pdev->dev)); |
| 2237 | |
| 2238 | /* set up pointers to ram-based 'registers' */ |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2239 | device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0; |
| 2240 | device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1; |
| 2241 | device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2; |
| 2242 | device->tspp_key_table = device->base + TSPP_DATA_KEY; |
| 2243 | device->tspp_global_performance = |
| 2244 | device->base + TSPP_GLOBAL_PERFORMANCE; |
| 2245 | device->tspp_pipe_context = |
| 2246 | device->base + TSPP_PIPE_CONTEXT; |
| 2247 | device->tspp_pipe_performance = |
| 2248 | device->base + TSPP_PIPE_PERFORMANCE; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2249 | |
| 2250 | device->bam_props.summing_threshold = 0x10; |
| 2251 | device->bam_props.irq = device->bam_irq; |
| 2252 | device->bam_props.manage = SPS_BAM_MGR_LOCAL; |
| 2253 | |
| 2254 | if (sps_register_bam_device(&device->bam_props, |
| 2255 | &device->bam_handle) != 0) { |
| 2256 | pr_err("tspp: failed to register bam"); |
| 2257 | goto err_bam; |
| 2258 | } |
| 2259 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2260 | if (tspp_clock_start(device) != 0) { |
| 2261 | dev_err(&pdev->dev, "Can't start clocks"); |
| 2262 | goto err_clock; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | spin_lock_init(&device->spinlock); |
| 2266 | tasklet_init(&device->tlet, tspp_sps_complete_tlet, |
| 2267 | (unsigned long)device); |
| 2268 | |
| 2269 | /* initialize everything to a known state */ |
| 2270 | tspp_global_reset(device); |
| 2271 | |
| 2272 | version = readl_relaxed(device->base + TSPP_VERSION); |
| 2273 | if (version != 1) |
| 2274 | pr_warn("tspp: unrecognized hw version=%i", version); |
| 2275 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2276 | /* initialize the channels */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2277 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2278 | if (tspp_channel_init(&(device->channels[i]), device) != 0) { |
| 2279 | pr_err("tspp_channel_init failed"); |
| 2280 | goto err_channel; |
| 2281 | } |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2284 | /* stop the clocks for power savings */ |
| 2285 | tspp_clock_stop(device); |
| 2286 | |
| 2287 | /* everything is ok, so add the device to the list */ |
| 2288 | list_add_tail(&(device->devlist), &tspp_devices); |
| 2289 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2290 | return 0; |
| 2291 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2292 | err_channel: |
| 2293 | err_clock: |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2294 | sps_deregister_bam_device(device->bam_handle); |
| 2295 | err_bam: |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2296 | tspp_debugfs_exit(device); |
| 2297 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 2298 | tsif_debugfs_exit(&device->tsif[i]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2299 | err_gpio: |
| 2300 | err_irq: |
| 2301 | tspp_stop_gpios(device); |
| 2302 | iounmap(device->bam_props.virt_addr); |
| 2303 | err_map_bam: |
| 2304 | err_res_bam: |
| 2305 | iounmap(device->base); |
| 2306 | err_map_dev: |
| 2307 | err_res_dev: |
| 2308 | iounmap(device->tsif[1].base); |
| 2309 | err_map_tsif1: |
| 2310 | err_res_tsif1: |
| 2311 | iounmap(device->tsif[0].base); |
| 2312 | err_map_tsif0: |
| 2313 | err_res_tsif0: |
| 2314 | if (device->tsif_ref_clk) |
| 2315 | clk_put(device->tsif_ref_clk); |
| 2316 | err_refclock: |
| 2317 | if (device->tsif_pclk) |
| 2318 | clk_put(device->tsif_pclk); |
| 2319 | err_pclock: |
| 2320 | kfree(device); |
| 2321 | |
| 2322 | out: |
| 2323 | return rc; |
| 2324 | } |
| 2325 | |
| 2326 | static int __devexit msm_tspp_remove(struct platform_device *pdev) |
| 2327 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2328 | struct tspp_channel *channel; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2329 | u32 i; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2330 | |
| 2331 | struct tspp_device *device = platform_get_drvdata(pdev); |
| 2332 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2333 | /* free the buffers, and delete the channels */ |
| 2334 | for (i = 0; i < TSPP_NUM_CHANNELS; i++) { |
| 2335 | channel = &device->channels[i]; |
| 2336 | tspp_close_channel(device->pdev->id, i); |
| 2337 | device_destroy(tspp_class, channel->cdev.dev); |
| 2338 | cdev_del(&channel->cdev); |
| 2339 | } |
| 2340 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2341 | sps_deregister_bam_device(device->bam_handle); |
| 2342 | |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2343 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 2344 | tsif_debugfs_exit(&device->tsif[i]); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2345 | |
| 2346 | wake_lock_destroy(&device->wake_lock); |
| 2347 | free_irq(device->tspp_irq, device); |
| 2348 | tspp_stop_gpios(device); |
| 2349 | |
| 2350 | iounmap(device->bam_props.virt_addr); |
| 2351 | iounmap(device->base); |
| 2352 | for (i = 0; i < TSPP_TSIF_INSTANCES; i++) |
| 2353 | iounmap(device->tsif[i].base); |
| 2354 | |
| 2355 | if (device->tsif_ref_clk) |
| 2356 | clk_put(device->tsif_ref_clk); |
| 2357 | |
| 2358 | if (device->tsif_pclk) |
| 2359 | clk_put(device->tsif_pclk); |
| 2360 | |
| 2361 | pm_runtime_disable(&pdev->dev); |
| 2362 | pm_runtime_put(&pdev->dev); |
| 2363 | kfree(device); |
| 2364 | |
| 2365 | return 0; |
| 2366 | } |
| 2367 | |
| 2368 | /*** power management ***/ |
| 2369 | |
| 2370 | static int tspp_runtime_suspend(struct device *dev) |
| 2371 | { |
| 2372 | dev_dbg(dev, "pm_runtime: suspending..."); |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
| 2376 | static int tspp_runtime_resume(struct device *dev) |
| 2377 | { |
| 2378 | dev_dbg(dev, "pm_runtime: resuming..."); |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
| 2382 | static const struct dev_pm_ops tspp_dev_pm_ops = { |
| 2383 | .runtime_suspend = tspp_runtime_suspend, |
| 2384 | .runtime_resume = tspp_runtime_resume, |
| 2385 | }; |
| 2386 | |
| 2387 | static struct platform_driver msm_tspp_driver = { |
| 2388 | .probe = msm_tspp_probe, |
| 2389 | .remove = __exit_p(msm_tspp_remove), |
| 2390 | .driver = { |
| 2391 | .name = "msm_tspp", |
| 2392 | .pm = &tspp_dev_pm_ops, |
| 2393 | }, |
| 2394 | }; |
| 2395 | |
| 2396 | |
| 2397 | static int __init mod_init(void) |
| 2398 | { |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2399 | int rc; |
| 2400 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2401 | /* make the char devs (channels) */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2402 | rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp"); |
| 2403 | if (rc) { |
| 2404 | pr_err("tspp: alloc_chrdev_region failed: %d", rc); |
| 2405 | goto err_devrgn; |
| 2406 | } |
| 2407 | |
| 2408 | tspp_class = class_create(THIS_MODULE, "tspp"); |
| 2409 | if (IS_ERR(tspp_class)) { |
| 2410 | rc = PTR_ERR(tspp_class); |
| 2411 | pr_err("tspp: Error creating class: %d", rc); |
| 2412 | goto err_class; |
| 2413 | } |
| 2414 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2415 | /* register the driver, and check hardware */ |
| 2416 | rc = platform_driver_register(&msm_tspp_driver); |
| 2417 | if (rc) { |
| 2418 | pr_err("tspp: platform_driver_register failed: %d", rc); |
| 2419 | goto err_register; |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | return 0; |
| 2423 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2424 | err_register: |
| 2425 | class_destroy(tspp_class); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2426 | err_class: |
| 2427 | unregister_chrdev_region(0, TSPP_NUM_CHANNELS); |
| 2428 | err_devrgn: |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2429 | return rc; |
| 2430 | } |
| 2431 | |
| 2432 | static void __exit mod_exit(void) |
| 2433 | { |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2434 | /* delete low level driver */ |
| 2435 | platform_driver_unregister(&msm_tspp_driver); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2436 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2437 | /* delete upper layer interface */ |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2438 | class_destroy(tspp_class); |
| 2439 | unregister_chrdev_region(0, TSPP_NUM_CHANNELS); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | module_init(mod_init); |
| 2443 | module_exit(mod_exit); |
| 2444 | |
Joel Nider | 435ad8e | 2011-12-14 16:53:30 +0200 | [diff] [blame] | 2445 | MODULE_DESCRIPTION("TSPP platform device and char dev"); |
Joel Nider | 5556a85 | 2011-10-16 10:52:13 +0200 | [diff] [blame] | 2446 | MODULE_LICENSE("GPL v2"); |